Avoid specifying all arguments in a subclass

前端 未结 4 1911
悲哀的现实
悲哀的现实 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:29

    Are you wanting something like this?

    class A(object):
        def __init__(self, a, b, c, d, e, f, g):
            # do stuff
            print a, d, g
    
    class B(A):
        def __init__(self, *args):
            args = list(args)
            self.__W = args.pop()
            A.__init__(self, *args)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-09 04:37

    Edit: based on Matt's suggestion, and to address gnibbler's concern re a positional-argument approach; you might check to make sure that the additional subclass-specific argument is being specified—similar to Alex's answer:

    class B(A):
      def __init__(self, *args, **kwargs):
        try:
          self._w = kwargs.pop('w')
        except KeyError:
          pass
        super(B,self).__init__(*args, **kwargs)
    
    >>> b = B(1,2,w=3)
    >>> b.a
    1
    >>> b.b
    2
    >>> b._w
    3
    

    Original answer:
    Same idea as Matt's answer, using super() instead.

    Use super() to call superclass's __init__() method, then continue initialising the subclass:

    class A(object):
      def __init__(self, a, b):
        self.a = a
        self.b = b
    
    class B(A):
      def __init__(self, w, *args):
        super(B,self).__init__(*args)
        self.w = w
    
    0 讨论(0)
  • 2020-12-09 04:38

    In situations where some or all of the arguments passed to __init__ have default values, it can be useful to avoid repeating the __init__ method signature in subclasses.

    In these cases, __init__ can pass any extra arguments to another method, which subclasses can override:

    class A(object):
        def __init__(self, a=1, b=2, c=3, d=4, *args, **kwargs):
            self.a = a
            self.b = b
            # …
            self._init_extra(*args, **kwargs)
    
        def _init_extra(self):
            """
            Subclasses can override this method to support extra
            __init__ arguments.
            """
    
            pass
    
    
    class B(A):
        def _init_extra(self, w):
            self.w = w
    
    0 讨论(0)
提交回复
热议问题