程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python @staticmethod and @classmethod

編輯:Python

author : Bad make
Potential creative stars Huawei cloud sharing expert
Blog records learning ideas , Items and errors , Looking for like-minded friends
If you think it helps, remember to click three times ┗|`O′|┛ Ow ~~


introduction

This article is my right to python The static method inside (staticmethod) and Class method (classmethod) Some insights , I hope I can help you . Before preparing to write this article, I was also very confused , Always don't understand , muddle along , Just understand the code , It's not often used anyway , I didn't care . But recently, due to the needs of the project , The full screen static methods and class methods are true, which makes me a little confused , What do I do , Want to continue , Then you have to learn . Don't push yourself , Never know your potential .

A relatively simple and rough way to distinguish : To decide whether to use @staticmethod still @classmethod, You have to look inside class methods . If your method accesses other variables in the class / Method , So use @classmethod. On the other hand , If the method does not touch any other variables in the class , So use @staticmethod.
But its design has a deeper logical connection :
classmethod Mainly used to replace constructors .
staticmethod Do not use the state of the object , Don't even use the structure of the class itself . It can be a function outside a class . It is only placed inside the class , Used to group functions with similar functions ( for example , image Java Mathematical static methods )

@classmethod
Person.printAge = classmethod(Person.printAge)

@staticmethod
Calculator.add_numbers = staticmethod(Calculator.add_numbers)

Can understand these two lines of code , You should be able to understand what static methods are , What is a class method . Of course , It doesn't matter if you don't understand , Let's explain in detail through chestnuts \

@classmethod

What is a class method ?

What is a class method ?
A class method is a method bound to a class rather than its object . It does not need to create class instances , It's like staticmethod equally .

The difference between static methods and class methods is :

Static methods know nothing about classes , Process parameters only
Class methods apply to classes , Because its parameters are always the class itself .
Class methods can be called by classes and their objects .

Class.classmethod()
Class().classmethod()

But anyway ,class Methods are always attached to a class , The first parameter is the class itself .cls

def classMethod(cls, args…)


Example 1

Code :


class Person:
age = 25
def printAge(cls):
print('The age is:', cls.age)
Person.printAge = classmethod(Person.printAge)
Person.printAge()

result :


Code :

class Person:
age = 25
@classmethod
def printAge(cls):
print('The age is:', cls.age)
Person.printAge()

result :

You will find that the code has changed , But the result is the same . If you are careful, you will find , you 're right , In code
****@classmethod Equivalent to Person.printAge = classmethod(Person.printAge)

Instance methods can call instance methods 、 Class method 、 Static methods . Class objects can only call class methods 、 Static methods .

classmethod And staticmethod Usage is similar. ,classmethod Can pass staticmethod Instead of , When called through a class , The two are indistinguishable from each other for the caller . The difference between the two is that ,classmethod Added a reference to the actual calling class .
🥭🥭 Method can determine that it is called through the base class , Or is it called through a subclass
🥭🥭 When called through subclasses , Method can return instances of subclasses instead of base classes
🥭🥭 When called through subclasses , Method can call other of the subclass classmethod







If you think it's useful, you can give it to three companies , Pay attention to a wave !!! Take you to learn more python Little knowledge


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved