📄 75.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -> Class Instances</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="72.html" class="navtitle">7. Classes and Object-Oriented Programming</a> > <span class="nonavtitle">Class Instances</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="74.html" title="The class statement"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=75" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="75.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="76.html" title="Reference Counting and Instance Destruction"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A00%3A22+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105086039026000243077235</font><a href="read3.asp?bookname=0735710910&snode=75&now=5%2F28%2F2002+9%3A00%3A22+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Class Instances</h3>
<p>Instances of a class are created by calling a class object as a function. This creates a new instance and calls the <tT CLAss="monofont">_ _init_ _()</tt> method of the class (if defined). For example:</P>
<PRE>
# Create a few accounts
a = Account("Guido", 1000.00) # Invokes Account._ _init_ _(a,"Guido",1000.00)
b = Account("Bill", 100000000000L) </pre>
<p>Once created, the attributes and methods of the newly created instances are accessible using the dot (<TT CLass="monofont">.</tt>) operator as follows:</p>
<pre>
a.deposit(100.00) # Call Account.deposit(a,100.00)
b.withdraw(sys.maxint) # Call Account.withdraw(b,sys.maxint)
name = a.name # Get account name
print a.account_type # Print account type </pre>
<p>Internally, each instance is implemented using a dictionary that抯 accessible as the instance抯 <tt class="monofont">_ _dict_ _</Tt> attribute (described in detail in <a Href="33.html">Chapter 3</A>).This dictionary contains the information that抯 unique to each instance. For example:</p>
<pre>
>>> print a._ _dict_ _
{'balance': 1100.0, 'name': 'Guido'}
>>> print b._ _dict_ _
{'balance': 97852516353L, 'name': 'Bill'} </Pre>
<p>Whenever the attributes of an instance are modified, these changes are made to the instance抯 local dictionary. Within methods defined in the class, attributes are changed through assignment to the <TT CLass="monofont">self</tT> variable as shown in the <TT Class="monofont">_ _init_ _()</TT>, <TT clasS="monofont">deposit()</TT>, and <Tt class="monofont">withdraw()</tt> methods of <tt class="monofont">Account</tt>. However, new attributes can be added to an instance at any time, like this:</p>
<prE>
a.number = 123456 # Add attribute 'number' to a._ _dict_ _ </prE>
<p>Although the assignment of attributes is always performed on the local dictionary of an instance, attribute access is somewhat more complicated. Whenever an attribute is accessed, the interpreter first searches the dictionary of the instance. If no match is found, the interpreter searches the dictionary of the class object used to create the instance. If this fails, a search of base classes is performed. (See the later section
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -