⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 107.html

📁 Python Ebook Python&XML
💻 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 Developer's Handbook -&gt; Methods Handling</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.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="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="102.html" class="navtitle">5. Object-Oriented Programming</a> &gt; <span class="nonavtitle">Methods Handling</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="106.html" title="Python Classes and Instances"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=107" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="107.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="108.html" title="Special Methods"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A33%3A25+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147039203230084227186105</font><a href="read0.asp?bookname=0672319942&snode=107&now=5%2F31%2F2002+4%3A33%3A25+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Methods Handling</h3>
				<p>Whenever you have to write methods in your classes, always keep in mind that the namespace searching order for attributes and methods is instance, class, and base classes; and don't forget that <tT CLAss="monofont">self</tt> is always the first or only argument to be used in method headers.</P>

				<H4>Accessing Unbounded Methods</H4>
					<P>The next example shows what you should do in order to unbind a class method and use it outside the class definition.</p>

					<pre>
						
1: obj = classname()
2: umethod = classname.methodname()
3: umethod(obj, args)

					</PRE>

					<P>Line 1: Creates a class instance object.</p>

					<p>Line 2: Creates an object that references the class method. The method is still unattached to the object at this stage.</p>

					<p>Line 3: Executes the class method by transporting the instance reference (<tt class="monofont">obj</tt>) and the list of arguments (<tt class="monofont">args</Tt>).</p>

					<P>Note that the first argument to an unbound method must be an instance of the correct class, or an exception will be thrown.<a naMe="idx1073743383"></a><a nAme="idx1073743384"></a><A NAMe="idx1073743385"></a><a nAME="idx1073743386"></A><a namE="idx1073743387"></A><A Name="idx1073743388"></a><A NAMe="idx1073743389"></a></p>

				
				<h4>Handling Global Class Variables</h4>
					<p>The next example defines a function that prints a class variable. Every time a new instance is created, <tt class="monofont">Globalcount</tt> increases.</p>

					<pre>
						
&gt;&gt;&gt; def printGlobalcount():

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -