Learn python class methods and object methods, and learn python class objects

Source: Internet
Author: User

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

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.