Difference between staticmethod and classmethod

后端 未结 28 2087
一整个雨季
一整个雨季 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:48

    A quick hack-up ofotherwise identical methods in iPython reveals that @staticmethod yields marginal performance gains (in the nanoseconds), but otherwise it seems to serve no function. Also, any performance gains will probably be wiped out by the additional work of processing the method through staticmethod() during compilation (which happens prior to any code execution when you run a script).

    For the sake of code readability I'd avoid @staticmethod unless your method will be used for loads of work, where the nanoseconds count.

提交回复
热议问题