super() and @staticmethod interaction

后端 未结 3 1436
醉梦人生
醉梦人生 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:23

    When you call a normal method on a object instance, the method receives the object instance as first parameter. It can get the class of tte object and its parent class, so it makes sense to call super.

    When you call a classmethod method on an object instance or on a class, the method receives the class as first parameter. It can get the parent class, so it makes sense to call super.

    But when you call a staticmethod method, the method does not receive anything and has no way to know from what object or class it was called. That's the reason why you cannot access super in a staticmethod.

提交回复
热议问题