将外部函数分配给 Python 中的 class 变量
Assign external function to class variable in Python
我正在尝试将别处定义的函数分配给 class 变量,以便稍后可以在实例的方法之一中调用它,如下所示:
from module import my_func
class Bar(object):
func = my_func
def run(self):
self.func() # Runs my function
问题是这失败了,因为在执行 self.func()
时,实例作为第一个参数传递。
我想出了一个 hack,但对我来说看起来很丑,有人有替代方案吗?
In [1]: class Foo(object):
...: func = lambda *args: args
...: def __init__(self):
...: print(self.func())
...:
In [2]: class Foo2(object):
...: funcs = [lambda *args: args]
...: def __init__(self):
...: print(self.funcs[0]())
...:
In [3]: f = Foo()
(<__main__.Foo object at 0x00000000044BFB70>,)
In [4]: f2 = Foo2()
()
编辑:行为与内置函数不同!
In [13]: from math import pow
In [14]: def pow_(a, b):
....: return pow(a, b)
....:
In [15]: class Foo3(object):
....: func = pow_
....: def __init__(self):
....: print(self.func(2, 3))
....:
In [16]: f3 = Foo3()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-c27c8778655e> in <module>()
----> 1 f3 = Foo3()
<ipython-input-15-efeb6adb211c> in __init__(self)
2 func = pow_
3 def __init__(self):
----> 4 print(self.func(2, 3))
5
TypeError: pow_() takes exactly 2 arguments (3 given)
In [17]: class Foo4(object):
....: func = pow
....: def __init__(self):
....: print(self.func(2, 3))
....:
In [18]: f4 = Foo4()
8.0
Python 函数是 descriptor objects,当 class 上的属性访问它们时,一个实例会导致它们被绑定为方法。
如果您想防止这种情况发生,请使用 staticmethod
function 将函数包装在不绑定到实例的不同描述符中:
class Bar(object):
func = staticmethod(my_func)
def run(self):
self.func()
或者,通过方法的 __func__
属性访问未绑定函数:
def run(self):
self.func.__func__()
或直接转到 class __dict__
属性以完全绕过描述符协议:
def run(self):
Bar.__dict__['func']()
至于 math.pow
,那不是 Python 函数,因为它是用 C 代码编写的。大多数内置函数是用 C 编写的,并且大多数不是描述符。
我正在尝试将别处定义的函数分配给 class 变量,以便稍后可以在实例的方法之一中调用它,如下所示:
from module import my_func
class Bar(object):
func = my_func
def run(self):
self.func() # Runs my function
问题是这失败了,因为在执行 self.func()
时,实例作为第一个参数传递。
我想出了一个 hack,但对我来说看起来很丑,有人有替代方案吗?
In [1]: class Foo(object):
...: func = lambda *args: args
...: def __init__(self):
...: print(self.func())
...:
In [2]: class Foo2(object):
...: funcs = [lambda *args: args]
...: def __init__(self):
...: print(self.funcs[0]())
...:
In [3]: f = Foo()
(<__main__.Foo object at 0x00000000044BFB70>,)
In [4]: f2 = Foo2()
()
编辑:行为与内置函数不同!
In [13]: from math import pow
In [14]: def pow_(a, b):
....: return pow(a, b)
....:
In [15]: class Foo3(object):
....: func = pow_
....: def __init__(self):
....: print(self.func(2, 3))
....:
In [16]: f3 = Foo3()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-c27c8778655e> in <module>()
----> 1 f3 = Foo3()
<ipython-input-15-efeb6adb211c> in __init__(self)
2 func = pow_
3 def __init__(self):
----> 4 print(self.func(2, 3))
5
TypeError: pow_() takes exactly 2 arguments (3 given)
In [17]: class Foo4(object):
....: func = pow
....: def __init__(self):
....: print(self.func(2, 3))
....:
In [18]: f4 = Foo4()
8.0
Python 函数是 descriptor objects,当 class 上的属性访问它们时,一个实例会导致它们被绑定为方法。
如果您想防止这种情况发生,请使用 staticmethod
function 将函数包装在不绑定到实例的不同描述符中:
class Bar(object):
func = staticmethod(my_func)
def run(self):
self.func()
或者,通过方法的 __func__
属性访问未绑定函数:
def run(self):
self.func.__func__()
或直接转到 class __dict__
属性以完全绕过描述符协议:
def run(self):
Bar.__dict__['func']()
至于 math.pow
,那不是 Python 函数,因为它是用 C 代码编写的。大多数内置函数是用 C 编写的,并且大多数不是描述符。