Python object-oriented-class properties and instance properties

Source: Internet
Author: User

Property: is a data or function element that belongs to an object

Classes have class methods, instance methods, static methods, class data properties (class variables), and instance data properties (instance variables).

Class properties: Include class methods and class variables that can be accessed through a class or instance and modified only by a class.

Instance properties: including instance methods and instance variables

classMyClass (object): Name='ANL'    def __init__(self, Age): Self.age=Age @classmethoddefClass_method (CLS):Print "I ' m class method"    defExample_method (self): Self.age= 20Print "I ' m a method, and age is%d", Self.age @staticmethoddefStatic_method ():Print "I ' m static method"

Using classes to access class variables

Myclass.name   # results for ' Anl '

Using classes to modify class variables

' Delav '   pritn myclass.name   # results for ' Delav '

Utility classes to access class methods

Myclass.class_method ()   # result for I ' m class method

Using classes to access static methods

Myclass.static_method ()   # result for I ' m static method

modifying instance variables

# instantiation of print ob.age     # results forob.age = # to       Modify instance Properties Print Ob.age     # Results of

Using instances to access class variables

print ob.name   # results for ' Delav '

Using instances to access instance methods

Ob.example_method    # results for I ' m example method, and age is

Using instances to access class methods

Ob.class_method ()    # result for I ' m class method

Using instances to access static methods

Ob.static_method ()   # result for I ' m static method

Modify instance properties, class variables unchanged, instance variables changed

' Bon '    Print Myclass.name  # results for Delavprint ob.name       # result for Bon

Total Results

Python object-oriented-class properties and instance properties

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.