super() and @staticmethod interaction

后端 未结 3 1433
醉梦人生
醉梦人生 2020-12-08 03:37

Is super() not meant to be used with staticmethods?

When I try something like

class First(object):
  @staticmethod
  def getlist():
    return [\'fir         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 04:37

    Since Second inherits form First, you can just use First.getlist() instead of passing in two arguments in super(i.e. super(Second, Second))

    class First(object):
       @staticmethod
       def getlist():
         return ['first']
    
    class Second(First):
      @staticmethod
      def getlist():
        # l = super(Second, Second).getlist()
        l = First.getlist()
        l.append('second')
        return l
    
    a = Second.getlist()
    print (a)
    

提交回复
热议问题