Avoid specifying all arguments in a subclass

前端 未结 4 1914
悲哀的现实
悲哀的现实 2020-12-09 04:07

I have a class:

class A(object):
    def __init__(self,a,b,c,d,e,f,g,...........,x,y,z)
        #do some init stuff

And I have a subclass w

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 04:30

    Considering that arguments could be passed either by name or by position, I'd code:

    class B(A):
        def __init__(self, *a, **k):
          if 'W' in k:
            w = k.pop('W')
          else:
            w = a.pop()
          A.__init__(self, *a, **k)
          self._W = w
    

提交回复
热议问题