简体   繁体   中英

How to make python decorator work with inherited classes

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'

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.

Remove the arguments you are passing into super, they are unnecessary. It should fix your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM