Reputation: 21
I have a piece of code like this and it doesn't work
I'm trying to give instances of some class a unique id here.
def countit(Cls):
class CC(Cls):
count = 0
def __init__(self,*args):
super(CC, self).__init__(*args)
self.id = CC.count
CC.count += 1
return CC
class BaseCls(object):
def __init__(self,x,y):
self.x=x
self.y=y
@countit
class Cls(BaseCls):
def __init__(self,x,y,z):
super(Cls, self).__init__(x,y)
self.z=z
c = Cls(1,2,3)
I got errors like this
Traceback (most recent call last):
File "test.py", line 55, in <module>
c = Cls(1,2,3)
File "test.py", line 35, in __init__
super(CC, self).__init__(*args)
File "test.py", line 48, in __init__
super(Cls, self).__init__(x,y)
TypeError: __init__() missing 1 required positional argument: 'z'
Upvotes: 0
Views: 89
Reputation: 36
Remove the arguments you are passing into super, they are unnecessary. It should fix your issue.
Upvotes: 0
Reputation: 4940
The error is because the decorator calls the constructor of the decorated class’s base with *args. In this case, z is unexpected.
Suggested approach for this is to use mixins instead of decorators if you want to add functionality to base and derived classes.
Upvotes: 1