This article is mainly about learning python class methods and object methods with everyone, starting from a simple example. Interested friends can refer to
The examples in this article are for python class methods and objects Method for study and research, the specific content is as follows
class Test_Demo: TEST = 'test_value' def __init__(self,name,age): self.name = name self.age = age #static method @staticmethod def test_static(): return Test_Demo.TEST #特性 @property def test_property(self): return self.name+':'+str(self.age) #类方法 @classmethod def test_class(self): return self.TEST if __name__ == '__main__': test_demo = Test_Demo('zj',23) #print(test_demo.name) print(Test_Demo.test_static()) print(test_demo.test_property) print(test_demo.test_class())
Output results:
Note: The difference from PHP is:
Class methods and static methods can access the static variables of the class (class variables, TEST), but they cannot access instance variables (i.e. name, age)
If accessed An error will be reported:
Is the above the entire content of this article? I hope it will be helpful to everyone's study.
For more articles related to the introduction of python class methods and object methods, please pay attention to the PHP Chinese website!