Python Class II: Properties of a class

Source: Internet
Author: User


The class has data properties and Method properties:


A property is a data member or a function member of an object.


The Data property of a class can be changed only by referencing the instance of the class, or by a method defined by the class that accesses the Data property (also by instantiating the class).


It can also be said that the data property of a class is bound to a class, and the data properties of the class are unaffected by any instantiated object.


There are two ways to access the properties of a class, one is Dir (), and the other is the Class.__dict__ property


Dir () can see a list of the names of the properties of the object.

The built-in properties of the __dict__ class can return a dictionary that corresponds to the property name key value. That is, the corresponding relationship between the object's property name and the property value is returned.


The following is a simple example of a different code for the class's Dir () and the class's built-in __dict__ method to see the properties:


[[Email protected] eg_4]# cat ! $cat  class_property.py#coding:utf-8class demo ( Object):     ' Demo for class property '     def __ init__ (Self,testname):        self.name = testname     def update_name (self,newname):         self.name  = newnameprint dir (Demo) print print demo.__dict__[[email protected] eg_4]#  [[email protected] eg_4]# [[email protected] eg_4]# [[email protected ] eg_4]# [[email protected] eg_4]# python2.7  class_property.py[' __class__ ' ,  ' __delattr__ ',  ' __dict__ ',  ' __doc__ ",  ' __format__ ',  ' __getattribute__ ',  ' __ Hash__ ',  ' __init__ ',  ' __module__ ',  "__new__ ',  ' __reduce__ ',  ' __reduce_ex__ ',  ' __ repr__ ',  ' __setattr__ ',  ' __sizeof__ ',  ' __str__ ',  ' __subclasshook__ ', ',  ' __weakref__ ',  ' update_name ']{' Update_name ': <function update_name at 0x7febc61b4c80>,  ' __module__ ':  ' __ main__ ',  ' __dict__ ': <attribute  ' __dict__ '  of  ' demo '  objects>,  ' __ weakref__ ': <attribute  ' __weakref__ '  of  ' demo '  objects>,  ' __doc__ ':  ' Demo for class property ',  ' __init__ ': <function __init__ at  0x7febc61b4c08>}


This article is from the Linux and networking blogs, so be sure to keep this source http://khaozi.blog.51cto.com/952782/1858503

Python Class II: Properties of a class

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.