I’m implementing several constructors using @classobj. I’m not only setting variables, but also calling methods in the new class:
class Example:
def __init__(self):
pass
@classmethod
def constructor1(cls,x,y):
self=cls
self.__x = x
self.__somemethod(self,y)
...
I get the following error:
unbound method __somemethod() must be called with Example instance as
first argument (got classobj instance instead)
How do I resolve this problem?
If you’re wanting your class method to be a constructor, you probably want to be creating an instance of the class you get passed in as
cls. I suspect you’re trying to do that with yourself = clsline, but you’re not actually creating a new instance because you’ve neglected to put parentheses. There are some other issues too, but I think that is the key one. Here’s a fixed constructor: