Difference between staticmethod and classmethod

后端 未结 28 2794
一整个雨季
一整个雨季 2020-11-21 06:11

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?

28条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 06:52

    Analyze @staticmethod literally providing different insights.

    A normal method of a class is an implicit dynamic method which takes the instance as first argument.
    In contrast, a staticmethod does not take the instance as first argument, so is called 'static'.

    A staticmethod is indeed such a normal function the same as those outside a class definition.
    It is luckily grouped into the class just in order to stand closer where it is applied, or you might scroll around to find it.

提交回复
热议问题