Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ref / attribute-access.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="ref.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="ref.html" title='Python Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="metaclasses.html" />
<link rel="prev" href="customization.html" />
<link rel="parent" href="specialnames.html" />
<link rel="next" href="new-style-attribute-access.html" />
<meta name='aesop' content='information' />
<title>3.3.2 Customizing attribute access</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.3.1 Basic customization"
href="customization.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3.3 Special method names"
href="specialnames.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.3.2.1 More attribute access"
href="new-style-attribute-access.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="customization.html">3.3.1 Basic customization</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="new-style-attribute-access.html">3.3.2.1 More attribute access</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION005320000000000000000"></A><A NAME="attribute-access"></A>
<BR>
3.3.2 Customizing attribute access
</H2>
<P>
The following methods can be defined to customize the meaning of
attribute access (use of, assignment to, or deletion of <code>x.name</code>)
for class instances.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-206' xml:id='l2h-206' class="method">__getattr__</tt></b>(</nobr></td>
<td><var>self, name</var>)</td></tr></table></dt>
<dd>
Called when an attribute lookup has not found the attribute in the
usual places (i.e. it is not an instance attribute nor is it found in
the class tree for <code>self</code>). <code>name</code> is the attribute name.
This method should return the (computed) attribute value or raise an
<tt class="exception">AttributeError</tt> exception.
<P>
Note that if the attribute is found through the normal mechanism,
<tt class="method">__getattr__()</tt> is not called. (This is an intentional
asymmetry between <tt class="method">__getattr__()</tt> and <tt class="method">__setattr__()</tt>.)
This is done both for efficiency reasons and because otherwise
<tt class="method">__setattr__()</tt> would have no way to access other attributes of
the instance. Note that at least for instance variables, you can fake
total control by not inserting any values in the instance attribute
dictionary (but instead inserting them in another object). See the
<tt class="method">__getattribute__()</tt> method below for a way to actually get
total control in new-style classes.
<a id='l2h-208' xml:id='l2h-208'></a></dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-209' xml:id='l2h-209' class="method">__setattr__</tt></b>(</nobr></td>
<td><var>self, name, value</var>)</td></tr></table></dt>
<dd>
Called when an attribute assignment is attempted. This is called
instead of the normal mechanism (i.e. store the value in the instance
dictionary). <var>name</var> is the attribute name, <var>value</var> is the
value to be assigned to it.
<P>
If <tt class="method">__setattr__()</tt> wants to assign to an instance attribute, it
should not simply execute "<tt class="samp">self.<var>name</var> = value</tt>" -- this
would cause a recursive call to itself. Instead, it should insert the
value in the dictionary of instance attributes, e.g.,
"<tt class="samp">self.__dict__[<var>name</var>] = value</tt>". For new-style classes,
rather than accessing the instance dictionary, it should call the base
class method with the same name, for example,
"<tt class="samp">object.__setattr__(self, name, value)</tt>".
<a id='l2h-211' xml:id='l2h-211'></a></dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-212' xml:id='l2h-212' class="method">__delattr__</tt></b>(</nobr></td>
<td><var>self, name</var>)</td></tr></table></dt>
<dd>
Like <tt class="method">__setattr__()</tt> but for attribute deletion instead of
assignment. This should only be implemented if "<tt class="samp">del
obj.<var>name</var></tt>" is meaningful for the object.
</dl>
<P>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="new-style-attribute-access.html">3.3.2.1 More attribute access for new-style classes</a>
<LI><A href="descriptors.html">3.3.2.2 Implementing Descriptors</a>
<LI><A href="descriptor-invocation.html">3.3.2.3 Invoking Descriptors</a>
<LI><A href="slots.html">3.3.2.4 __slots__</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.3.1 Basic customization"
href="customization.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3.3 Special method names"
href="specialnames.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.3.2.1 More attribute access"
href="new-style-attribute-access.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="customization.html">3.3.1 Basic customization</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="new-style-attribute-access.html">3.3.2.1 More attribute access</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>