Learn python class methods and object methods, and learn python class objects
This article provides an example of studying the class and object methods of python. The details are 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 # feature @ property def test_property (self): return self. name + ':' + str (self. age) # class method @ classmethod def test_class (self): return self. TESTif _ 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 result:
Note: Unlike php:
Class and static methods can be used to initialize static variables (class variables, TEST) of the class, but neither of them can access instance variables (that is, name, age)
If the access is successful, an error is returned:
Is the above all the content in this article, and I hope it will help you in your learning.
Articles you may be interested in:
- Analysis of instance methods, static methods, class methods, class variables, and instance variables in python
- An alternative method for implementing multi-line annotation in Python
- Python class and static methods
- Python class method _ init _ and _ del _ structure and structure Process Analysis
- Describes several common class methods in Python.
- How to read and write files and related file objects in Python Programming