setattr and getattr : Attribute « Class « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Class » Attribute 
11.2.1.setattr and getattr
# __getattribute__(self, name): Automatically called when the attribute name is accessed. 
# __getattr__(self, name): Automatically called when the attribute name is accessed and the object has no such attribute.
# __setattr__(self, name, value): Automatically called when an attempt is made to bind the attribute name to value.
# __delattr__(self, name): Automatically called when an attempt is made to delete the attribute name.


class Rectangle:
    def __init__(self):
        self.width = 0
        self.height = 0
    def __setattr__(self, name, value):
        if name == 'size':
            self.width, self.height = value
        else:
            self.__dict__[name= value
    def __getattr__(self, name):
        if name == 'size':
            return self.width, self.height
        else:
            raise AttributeError
11.2.Attribute
11.2.1.setattr and getattr
11.2.2.Attribute Reference
11.2.3.Modifying Class Attributes
11.2.4.Attributes are values associated with the object.
11.2.5.Instance Attributes versus Class Attributes
11.2.6.Use Caution When Accessing Class Attribute with Instance
11.2.7.Class Attributes More Persistent
11.2.8.Let us make our example more useful by giving P some attributes
11.2.9.Simple Attribute Lookup Example
11.2.10.hasattr(), getattr(), setattr(), delattr()
11.2.11.Class Employee with class attribute count.
11.2.12.Class that defines method __getattribute__ and __getattr__
11.2.13.Attach attributes to the class by assigning names to it outside of the original class
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.