在python中使用与实例和类方法相同的功能 [英] Using same function as instance and classmethod in python
问题描述
一个人可以做这样的事情:
One can do something like this:
class master:
@combomethod
def foo(param):
param.bar() # Param could be type as well as object
class slaveClass( master ):
@classmethod
def bar(cls):
print("This is class method")
slaveType = slaveClass
slaveType.foo()
class slaveInstance( master ):
def __init__(self, data):
self.data = data
def bar(self):
print("This is "+self.data+" method")
slaveType = slaveInstance("instance")
slaveType.foo()
combomethod
在 。
combomethod
is defined in "Creating a method that is simultaneously an instance and class method".
我的问题是,为什么就是这样,默认的第一个参数不能使用d作为comboclass的参数?或者至少,为什么我不能将对象作为第一个参数传递给classmethod?我知道classmethod和instancemethods之间的区别,我知道装饰器,但我可能不明白 @classmethod
和 self $ c是如何内置的$ c>参数传递完成。有技术限制吗?或者,为什么还没有内置
combomethod
?
My question is, why is it like this, that default first parameter can't be used as parameter of comboclass? Or at least, why can't I pass object to classmethod as the first parameter? I know the difference between classmethod and instancemethods, and I know decorators, but I might not understand how built-in @classmethod
and self
parameter passing is made. Is there a technical limitation? Or, why isn't combomethod
allready built in?
推荐答案
combomethod
在访问时不会创建方法对象,而是一个特殊包装的函数。像方法一样,每次访问都会创建一个新对象,在这种情况下是一个新功能对象。
combomethod
doesn't create a method object when accessed but a specially wrapped function. Like methods each access creates a new object, in this case a new function object.
class A:
def __init__(self):
self.data = 'instance'
@combomethod
def foo(param):
if isinstance(param, A):
print("This is an " + param.data + " method.")
elif param is A:
print("This is a class method.")
>>> a = A()
>>> A.foo
<function foo at 0x00CFE810>
>>> a.foo
<function foo at 0x00CFE858>
>>> A.foo()
This is a class method.
>>> a.foo()
This is an instance method.
每次访问都是新功能:
>>> A.foo is A.foo
False
>>> a.foo is a.foo
False
foo
实际上是 _wrapper
的伪装:
>>> A.foo.__code__.co_name
'_wrapper'
从类调用闭包时具有obj == None(请注意, self在这里指的是组合方法,它在 self.method
中引用了原始函数对象):
When called from a class the closure has obj == None (note that 'self' here refers to the combomethod, which has a reference to the original function object in self.method
):
>>> print(*zip(A.foo.__code__.co_freevars, A.foo.__closure__), sep='\n')
('obj', <cell at 0x011983F0: NoneType object at 0x1E1DF8F4>)
('self', <cell at 0x01198530: combomethod object at 0x00D29630>)
('objtype', <cell at 0x00D29D10: type object at 0x01196858>)
作为实例的属性调用时,obj是实例:
When called as the attribute of an instance, obj is the instance:
>>> print(*zip(a.foo.__code__.co_freevars, a.foo.__closure__), sep='\n')
('obj', <cell at 0x01198570: A object at 0x00D29FD0>)
('self', <cell at 0x01198530: combomethod object at 0x00D29630>)
('objtype', <cell at 0x00D29D10: type object at 0x01196858>)
以下是存储在comomethod中的原始函数:
Here is the original function stored in the combomethod:
>>> A.foo.__closure__[1].cell_contents.method
<function foo at 0x00D1CB70>
>>> A.foo.__closure__[1].cell_contents.method.__code__.co_name
'foo'
_wrapper
执行 self.method
,将给定obj值的类或实例作为第一个参数: / p>
_wrapper
executes self.method
with either the class or instance as the first argument given the value of obj:
if obj is not None:
return self.method(obj, *args, **kwargs)
else:
return self.method(objtype, *args, **kwargs)
这篇关于在python中使用与实例和类方法相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!