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 is defined in “Creating a method that is simultaneously an instance and class method“.
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?
combomethoddoesn’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.It’s new for each access:
foois really_wrapperin disguise: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):When called as the attribute of an instance, obj is the instance:
Here is the original function stored in the combomethod:
_wrapperexecutesself.methodwith either the class or instance as the first argument given the value of obj: