Python object-oriented instance properties and class properties

Source: Internet
Author: User
This article to share the content is about the Python object-oriented instance properties and class attributes, has a certain reference value, the need for friends can refer to

Because Python is a dynamic language, creating an instance from a class can bind properties arbitrarily.

The way to bind properties to an instance is through an instance variable, or through the self variable:

Class Student (object):    def __init__ (self,name):        self.name = names = Student (' Jeff ') print (s.name) #jeff

When we define a class property, this property is categorized as all but all instances of the class

can be accessed. To test:

>>> class Student (object): ...     name = ' Student ' ...>>> s = Student () >>> print (s.name) student>>> print (student.name) Student >>> s.name = ' Jeff ' >>>>>> print (s.name) jeff>>> print (student.name) student> >> del s.name>>> print (s.name) student>>> print (student.name) Student

As can be seen from the above example, when writing a program, do not use the same name for instance properties and class properties, because instance properties of the same name will mask the class properties, but when you delete the strength attribute, and then use the same name, access to the class property

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.