1. Binding method
1.1 method bound to class: @ classmethod decorator decoration method
@classmethod is used for classes, that is, binding to classes,
1. When the class calls the method, it will pass the class itself as the first parameter (self) of the class method
2. When the object calls the method, it will also pass the class itself as the first parameter (self) of the class method
3. When a class is called, it will pass in a class. When a subclass calls a class binding method in the parent class, it will pass in a subclass
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: vita class MySQL: def __init__(self,host,port): self.host=host self.port=port @classmethod def from_conf(cls): print(cls) conn=MySQL("127.0.0.1","3306") print(MySQL.from_conf) print(conn.from_conf) #Object can also be called, but the first parameter passed by default is still the class MySQL.from_conf() conn.from_conf() E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py <bound method MySQL.from_conf of <class '__main__.MySQL'>> <bound method MySQL.from_conf of <class '__main__.MySQL'>> <class '__main__.MySQL'> <class '__main__.MySQL'> Process finished with exit code 0
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: vita class MySQL: def __init__(self,host,port): self.host=host self.port=port @classmethod def from_conf(cls): print(cls) class mysql(MySQL): pass # Which class is called, which class is passed in. Here is the class of the subclass mysql mysql.from_conf() E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py <class '__main__.mysql'> Process finished with exit code 0
1.2 methods bound to objects: methods not decorated by any decorators
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: vita class MySQL: def __init__(self,host,port): self.host=host self.port=port @classmethod def from_conf(cls): print(cls) # Methods bound to objects def from_obj(cls): print(cls) obj = MySQL("","") print(obj.from_conf) # Methods bound to objects print(obj.from_obj) E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py <bound method MySQL.from_conf of <class '__main__.MySQL'>> <bound method MySQL.from_obj of <__main__.MySQL object at 0x0000027E6AD9EB00>> Process finished with exit code 0
2. Unbound method
Functions decorated with static method inside the class are unbound methods and ordinary functions
staticmethod is not bound to a class or object, and can be called by anyone. It has no effect of automatic value transfer.
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: vita class MySQL: def __init__(self,host,port): self.host=host self.port=port @staticmethod def from_conf(cls): print(cls) #return cls("127.0.0.1","3306") conn=MySQL("127.0.0.1","3306") MySQL.from_conf("class test") conn.from_conf("obj test") E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py class test obj test Process finished with exit code 0
"Unbound methods can also be hidden functions, which cannot be called externally" #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: vita class MySQL: def __init__(self,host,port): self.host=host self.port=port @staticmethod def __from_conf(cls): print(cls) #return cls("127.0.0.1","3306") conn=MySQL("127.0.0.1","3306") # "Both statements report the following errors. You can try it yourself" MySQL.from_conf("class test") conn.from_conf("obj test") E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py Traceback (most recent call last): File "E:/PythonProject/python-test/BasicGrammer/test.py", line 19, in <module> conn.from_conf("obj test") AttributeError: 'MySQL' object has no attribute 'from_conf' Process finished with exit code 1