相关疑难解决方法(0)

如何在Python中将方法作为参数传递

是否可以将方法作为参数传递给方法?

self.method2(self.method1)

def method1(self):
    return 'hello world'

def method2(self, methodToRun):
    result = methodToRun.call()
    return result
Run Code Online (Sandbox Code Playgroud)

python parameter-passing

165
推荐指数
6
解决办法
18万
查看次数

我可以将类方法和默认参数传递给另一个类方法

我想将类方法作为默认参数传递给另一个类方法,以便我可以将该方法重用为@classmethod:

@classmethod
class foo:
    def func1(self,x):
        do somthing;
    def func2(self, aFunc = self.func1):
        # make some a call to afunc
        afunc(4)
Run Code Online (Sandbox Code Playgroud)

这就是为什么在func2aFunc默认情况下调用该方法的原因self.func1,但是我可以从类外部调用这个相同的函数,并在输入处传递一个不同的函数.

我明白了:

NameError:未定义名称"self"

这是我的设置:

class transmissionLine:  
    def electricalLength(self, l=l0, f=f0, gamma=self.propagationConstant, deg=False):  
Run Code Online (Sandbox Code Playgroud)

但我希望能够electricalLength使用不同的函数调用gamma,例如:

transmissionLine().electricalLength (l, f, gamma=otherFunc)
Run Code Online (Sandbox Code Playgroud)

python class

6
推荐指数
1
解决办法
3506
查看次数

标签 统计

python ×2

class ×1

parameter-passing ×1