Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="metaclasses.html" /> | |
13 | <link rel="prev" href="customization.html" /> | |
14 | <link rel="parent" href="specialnames.html" /> | |
15 | <link rel="next" href="new-style-attribute-access.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.3.2 Customizing attribute access</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="3.3.1 Basic customization" | |
25 | href="customization.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="3.3 Special method names" | |
28 | href="specialnames.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="3.3.2.1 More attribute access" | |
31 | href="new-style-attribute-access.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Reference Manual</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="customization.html">3.3.1 Basic customization</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="new-style-attribute-access.html">3.3.2.1 More attribute access</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION005320000000000000000"></A><A NAME="attribute-access"></A> | |
56 | <BR> | |
57 | 3.3.2 Customizing attribute access | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | The following methods can be defined to customize the meaning of | |
62 | attribute access (use of, assignment to, or deletion of <code>x.name</code>) | |
63 | for class instances. | |
64 | ||
65 | <P> | |
66 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
67 | <td><nobr><b><tt id='l2h-206' xml:id='l2h-206' class="method">__getattr__</tt></b>(</nobr></td> | |
68 | <td><var>self, name</var>)</td></tr></table></dt> | |
69 | <dd> | |
70 | Called when an attribute lookup has not found the attribute in the | |
71 | usual places (i.e. it is not an instance attribute nor is it found in | |
72 | the class tree for <code>self</code>). <code>name</code> is the attribute name. | |
73 | This method should return the (computed) attribute value or raise an | |
74 | <tt class="exception">AttributeError</tt> exception. | |
75 | ||
76 | <P> | |
77 | Note that if the attribute is found through the normal mechanism, | |
78 | <tt class="method">__getattr__()</tt> is not called. (This is an intentional | |
79 | asymmetry between <tt class="method">__getattr__()</tt> and <tt class="method">__setattr__()</tt>.) | |
80 | This is done both for efficiency reasons and because otherwise | |
81 | <tt class="method">__setattr__()</tt> would have no way to access other attributes of | |
82 | the instance. Note that at least for instance variables, you can fake | |
83 | total control by not inserting any values in the instance attribute | |
84 | dictionary (but instead inserting them in another object). See the | |
85 | <tt class="method">__getattribute__()</tt> method below for a way to actually get | |
86 | total control in new-style classes. | |
87 | <a id='l2h-208' xml:id='l2h-208'></a></dl> | |
88 | ||
89 | <P> | |
90 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
91 | <td><nobr><b><tt id='l2h-209' xml:id='l2h-209' class="method">__setattr__</tt></b>(</nobr></td> | |
92 | <td><var>self, name, value</var>)</td></tr></table></dt> | |
93 | <dd> | |
94 | Called when an attribute assignment is attempted. This is called | |
95 | instead of the normal mechanism (i.e. store the value in the instance | |
96 | dictionary). <var>name</var> is the attribute name, <var>value</var> is the | |
97 | value to be assigned to it. | |
98 | ||
99 | <P> | |
100 | If <tt class="method">__setattr__()</tt> wants to assign to an instance attribute, it | |
101 | should not simply execute "<tt class="samp">self.<var>name</var> = value</tt>" -- this | |
102 | would cause a recursive call to itself. Instead, it should insert the | |
103 | value in the dictionary of instance attributes, e.g., | |
104 | "<tt class="samp">self.__dict__[<var>name</var>] = value</tt>". For new-style classes, | |
105 | rather than accessing the instance dictionary, it should call the base | |
106 | class method with the same name, for example, | |
107 | "<tt class="samp">object.__setattr__(self, name, value)</tt>". | |
108 | <a id='l2h-211' xml:id='l2h-211'></a></dl> | |
109 | ||
110 | <P> | |
111 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
112 | <td><nobr><b><tt id='l2h-212' xml:id='l2h-212' class="method">__delattr__</tt></b>(</nobr></td> | |
113 | <td><var>self, name</var>)</td></tr></table></dt> | |
114 | <dd> | |
115 | Like <tt class="method">__setattr__()</tt> but for attribute deletion instead of | |
116 | assignment. This should only be implemented if "<tt class="samp">del | |
117 | obj.<var>name</var></tt>" is meaningful for the object. | |
118 | </dl> | |
119 | ||
120 | <P> | |
121 | ||
122 | <p><br /></p><hr class='online-navigation' /> | |
123 | <div class='online-navigation'> | |
124 | <!--Table of Child-Links--> | |
125 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
126 | ||
127 | <UL CLASS="ChildLinks"> | |
128 | <LI><A href="new-style-attribute-access.html">3.3.2.1 More attribute access for new-style classes</a> | |
129 | <LI><A href="descriptors.html">3.3.2.2 Implementing Descriptors</a> | |
130 | <LI><A href="descriptor-invocation.html">3.3.2.3 Invoking Descriptors</a> | |
131 | <LI><A href="slots.html">3.3.2.4 __slots__</a> | |
132 | </ul> | |
133 | <!--End of Table of Child-Links--> | |
134 | </div> | |
135 | ||
136 | <DIV CLASS="navigation"> | |
137 | <div class='online-navigation'> | |
138 | <p></p><hr /> | |
139 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
140 | <tr> | |
141 | <td class='online-navigation'><a rel="prev" title="3.3.1 Basic customization" | |
142 | href="customization.html"><img src='../icons/previous.png' | |
143 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
144 | <td class='online-navigation'><a rel="parent" title="3.3 Special method names" | |
145 | href="specialnames.html"><img src='../icons/up.png' | |
146 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
147 | <td class='online-navigation'><a rel="next" title="3.3.2.1 More attribute access" | |
148 | href="new-style-attribute-access.html"><img src='../icons/next.png' | |
149 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
150 | <td align="center" width="100%">Python Reference Manual</td> | |
151 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
152 | href="contents.html"><img src='../icons/contents.png' | |
153 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
154 | <td class='online-navigation'><img src='../icons/blank.png' | |
155 | border='0' height='32' alt='' width='32' /></td> | |
156 | <td class='online-navigation'><a rel="index" title="Index" | |
157 | href="genindex.html"><img src='../icons/index.png' | |
158 | border='0' height='32' alt='Index' width='32' /></A></td> | |
159 | </tr></table> | |
160 | <div class='online-navigation'> | |
161 | <b class="navlabel">Previous:</b> | |
162 | <a class="sectref" rel="prev" href="customization.html">3.3.1 Basic customization</A> | |
163 | <b class="navlabel">Up:</b> | |
164 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> | |
165 | <b class="navlabel">Next:</b> | |
166 | <a class="sectref" rel="next" href="new-style-attribute-access.html">3.3.2.1 More attribute access</A> | |
167 | </div> | |
168 | </div> | |
169 | <hr /> | |
170 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
171 | </DIV> | |
172 | <!--End of Navigation Panel--> | |
173 | <ADDRESS> | |
174 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
175 | </ADDRESS> | |
176 | </BODY> | |
177 | </HTML> |