Python class method and object method learning, python Class Object Learning

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.