The path to Python learning-attributes

Source: Internet
Author: User

This article is mainly about sharing the settings of the properties inside the Pyton and getting the built-in functions using: Property Properties:

1.__getattr__ (self, name)
-Defines the behavior when a user attempts to acquire a nonexistent property
2.__getattribute__ (self, name)
-Defines the behavior when the properties of the class are accessed
3.__setattr__ (self, name, value)
-Defines the behavior when a property is set
4.__delattr__ (self, name)
-Defines the behavior when a property is deleted
The property of an object is actually managed by a dictionary table in the object, which can be viewed by object.__dict__, and returns a dictionary table that shows the value corresponding to the string and attribute of each property name;

The above sequence of built-in function calls is:

1. Set the current property, call the __setattr__ (self, Name, value) method to set the current property, if the property exists, directly set the value of the property, if the property does not exist, it will increase the property

2. Gets the value of the property, calls the __getattribute__ (self, name) method to get, if the property exists after the call, gets the value of the property directly, and if the property does not exist, the __getattr__ (self, name) method is called to get

3. Delete attribute call __delattr__ (self, name) method to delete property

It is not difficult to see the setting and invocation of an object's properties by running the following example:

#-*-Coding:utf-8-*-
"""

@author: Zzj

"""

Class PropertyTest:
def __init__ (self, size = 10):
Self.size = Size
def __getattribute__ (self, name):
Print ("Accessing properties of this class")
Return super (). __GETATTRIBUTE__ (name)
def __getattr__ (self, name):
Print ("This property does not exist")
def __setattr__ (self, Name, value):
Print ("Set current Properties")
Return super (). __SETATTR__ (name, value)
def __delattr (self, name):
Print ("Delete Current property")
Super (). __DELATTR__ (name)

def getsize (self):
Return self.size
def SetSize (self, value):
Self.size = value
def delsize (self):
Del self.size
X = Property (GetSize, SetSize, Delsize)


C = PropertyTest ()
Print (c.size)

c.x = 1
Print (c.size)
Print (c.x)

Del c.x
Print (c.size)

The path to Python learning-attributes

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.