Python object-oriented six class properties and instance properties

Source: Internet
Author: User

First, instance properties

Python is a dynamic language, and an instance created from a class can be arbitrarily bound to a property.

1>>>classStudent (object):2...def __init__(self, name):3... Self.name =Name # Properties required for each instance4 ... 5>>> s = Student ('Jack')6>>> S.score = 90 # Arbitrary binding properties7>>>S.name8 'Jack'9>>>S.scoreTen90

Second, class properties

The attribute is defined directly in class, which is the class attribute and belongs to the Student class.

1>>>classStudent (object):2... Name ='Jack'   #Class Properties3 ... 4>>> Student.name#Get class Properties5 'Jack'6>>> s =Student ()7>>> S.name#Get class Properties8 'Jack'9>>> S.name ='Mike'     #Adding instance PropertiesTen>>> S.name#Get Instance Properties One 'Mike' A>>> Student.name#Get class Properties - 'Jack'

When writing a program, never use the same name for instance properties and class properties, because instance properties of the same name will mask class properties, but when you delete an instance property and then use the same name, the class attribute is accessed.

Python object-oriented six 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.