Python class method and object method learning, python Class Object Learning
1 class Test_Demo: 2 TEST = 'test _ value' 3 4 def _ init _ (self, name, age): 5 self. name = name 6 self. age = age 7 # static method 8 @ staticmethod 9 def test_static (): 10 return Test_Demo.TEST11 # feature 12 @ property13 def test_property (self): 14 return self. name + ':' + str (self. age) 15 # Class Method 16 @ classmethod17 def test_class (self): 18 return self. TEST19 20 if _ name _ = '_ main _': 21 test_demo = Test_Demo ('zj', 23) 22 # print (test_demo.name) 23 print (Test_Demo.test_static () 24 print (test_demo.test_property) 25 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: