【问题标题】:How to pass a method that receives parameters as a parameter of another function in Python如何将接收参数的方法作为Python中另一个函数的参数传递
【发布时间】:2012-07-16 12:50:26
【问题描述】:

我知道这是有效的:

def printValue():
    print 'This is the printValue() method'

def callPrintValue(methodName):
    methodName()
    print 'This is the callPrintValue() method'

但是有没有办法将接收参数的方法作为另一个函数的参数传递?

这是不可能的:

def printValue(value):
    print 'This is the printValue() method. The value is %s'%(value)

def callPrintValue(methodName):
    methodName()
    print 'This is the callPrintValue() method'

这是我得到的堆栈跟踪:

This is the printValue() method. The value is dsdsd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in callPrintValue
TypeError: 'NoneType' object is not callable

【问题讨论】:

    标签: python parameter-passing


    【解决方案1】:

    有些人觉得lambda 丑陋,但在这种情况下它是一个有用的工具。无需修改callPrintValue() 的签名,您可以使用lambda 快速定义一个将参数绑定到printValue() 的新函数。您是否真的要这样做取决于许多因素,并且可能像其他人建议的那样添加*args 参数是可取的。不过,这是一个值得考虑的选择。以下内容无需修改您当前的代码即可工作:

    >>> callPrintValue(lambda: printValue('"Hello, I am a value"'))
    This is the printValue() method. The value is "Hello, I am a value"
    This is the callPrintValue() method
    

    【讨论】:

    • 我同意 lambda 可能是这里所要求的 (+1)。事实上,这就是在 GUI 编程中经常使用的方式(例如 tkinter),因为您无法修改执行回调的函数。
    【解决方案2】:
    def printValue(value):
        print 'This is the printValue() method. The value is %s'%(value)
    
    def callPrintValue(methodName, *args):
        methodName(*args)
        print 'This is the callPrintValue() method'
    

    那么你可以这样称呼它:

    callPrintValue(printValue, "Value to pass to printValue")
    

    这允许你传入任意数量的参数,并且所有参数都被传递给你在callPrintValue中调用的函数

    【讨论】:

      【解决方案3】:

      我猜你可以做到这一点

      
      def callPrintValue(methodName, *args):
          methodName(*args)
          print 'This is the callPrintValue() method'
      
      

      拨打电话

      
      callPrintValue(printValue, "abc")
      

      【讨论】:

        【解决方案4】:

        你想使用元组解包:

        def print_value(*values):
            print values
        
        def call_print_value(func,args=None):
            func(*args)
        
        call_print_value(print_value,args=('this','works')) #prints ('this', 'works')
        

        从 API 的角度来看,我更喜欢将传递的参数保留为单独的关键字。 (然后更明确一点,print_value 使用了哪些参数,call_print_value 使用了哪些参数)。另请注意,在 python 中,习惯上将函数(和方法)名称命名为name_with_underscores。 CamelCase 通常用于类名。

        【讨论】:

          【解决方案5】:

          作为已经提供的答案的后续,您可能需要查看有关 stackoverflow 的以下问题,以便更好地理解 python 中的 *args 和/或 **kwargs 和 lambda

          1. What does *args and **kwargs mean?
          2. What does ** (double star) and * (star) do for python parameters?
          3. Python Lambda - why?

          【讨论】:

            猜你喜欢
            • 2012-01-28
            • 1970-01-01
            • 2019-10-09
            • 2013-11-19
            • 1970-01-01
            • 2017-08-07
            • 2022-01-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多