简体   繁体   English

在python中用作参数

[英]Function as argument in python

I need to pass some function as argument in another function. 我需要将某些函数作为参数传递给另一个函数。 So, I want to add current time to class attribute every second, for example 因此,例如,我想每秒将当前时间添加到class属性

import time
class Measurement():
    values = []
    def add_value(self, value):
        print "added value"
        self.values.append(value)


def smart_delay(input_function,args):
    start_time = time.time()
    while 5 > (time.time() -  start_time):
        print "Calling function"
        input_function(args)
        time.sleep(1)

measurement = Measurement()

smart_delay(measurement.add_value,time.time())

Ok, but after checking contents of measurement.values, I get [1425980407.173, 1425980407.173, 1425980407.173, 1425980407.173] - so values are the same!!! 好的,但是在检查了[1425980407.173, 1425980407.173, 1425980407.173, 1425980407.173]内容之后,我得到了[1425980407.173, 1425980407.173, 1425980407.173, 1425980407.173] -因此值是相同的! What happened? 发生了什么? And how to get proper values? 以及如何获得适当的价值?

Updated: 更新:

Actually, this question is about the way to allow to call some function, passed as the argument to another function. 实际上,这个问题是关于允许调用某个函数的方式,该函数作为参数传递给另一个函数。 What do you think about this: 你怎么看待这件事:

import time

class Measurement():
    values = []
    def add_value(self, value):
        print "added value"
        self.values.append(value)


def smart_delay(input_function):
    start_time = time.time()
    while 5 > (time.time() -  start_time):
        print "Calling function"
        input_function()
        time.sleep(1)

measurement = Measurement()

smart_delay(lambda: measurement.add_value(time.time()))

Your call to time.time() is executed before the call to smart_delay(...) , so smart_delay(measurement.addvalue, time.time()) will first get the return value from time.time() and pass that forward to smart_delay . 您对time.time()的调用在对smart_delay(...)的调用之前执行,因此smart_delay(measurement.addvalue, time.time())将首先从time.time()获得返回值并将其传递给到smart_delay

You need to pass the time.time function itself, and call it inside of the smart_delay method, instead of passing its return value: 您需要传递time.time函数本身,并在smart_delay方法内部调用它,而不是传递其返回值:

import time
class Measurement():
    values = []
    def add_value(self, value):
        print "added value"
        self.values.append(value)


def smart_delay(output_f, input_f):
    start_time = time.time()
    while 5 > (time.time() -  start_time):
        print "Calling function"
        output_f(input_f())
        time.sleep(1)

measurement = Measurement()

smart_delay(measurement.add_value, time.time)

Notice, that this is not the best way to do what you're doing, but it works. 注意,这不是做您正在做的事的最佳方法,但是它可以工作。


Here's how I'd do it: 这是我的处理方式:

import time

# Why do you need a measurement class which only acts as a list anyways?
my_measurements = []

def repeat(duration, function, args=(), kwargs={}, interval=1):
    """Repeat a function call for the given duration."""
    start_time = time.time()
    while duration > time.time() - start_time:
        function(*args, **kwargs)
        time.sleep(interval)

def add_time_measurement(measurements):
    measurements.append(time.time())

repeat(5, add_time_measurement, (my_measurements,))

And if you want some prints, you can just add them in the add_time_measurement function. 而且,如果您需要一些打印件,则可以将它们添加到add_time_measurement函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM