Consider this example:
class master:
@classmethod
def foo(cls):
cls.bar()
class slaveClass( master ):
@classmethod
def bar(cls):
print("This is class method")
slaveType = slaveClass
slaveType.foo()
class slaveInstance( master ):
#def foo(self):
# self.foo()
def __init__(self,data):
self.data=data
print("Instance has been made")
def bar(self):
print("This is "+self.data+" method")
slaveType = slaveInstance("instance")
slaveType.foo()
I know it works when last definition of foo is uncommented, but is there any other way to use this foo function without changing the usage. I have large project where classes defined the way things worked and I was able to change the way with slaveType but there happen to be a case where instance is needed, and there is bit too many foo like functions to be overridden for instance behavior.
Thank you stackers!
foois a classmethod, so simply never receives the instance - only the class. There's no way it could then call an instance method.foodefinition I will never call the classmethod, as it is overridden by instance method. I want to know, if there is any other way. I am able to change the master class but I need to keep the wayslaveTypewas used.slaveClassis a child class that inherits classmethodfoo()from Master.slaveInstanceis a grandchild class (not an instance) that then overridesfoo()as an instance method. (Btw, style points to avoid confusion: class names should be CamelCase, and never include 'Class' (this ain't Java) orInstanceorType(just plain wrong, in a class name). So:slaveClassshould simply beSlave, and 'master` beMaster.slaveTypeis not a type, it's an instance...