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

📄 110.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; Polymorphism</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">Polymorphism</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="109.html" title="Inheritance"><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=110" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="110.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="111.html" title="Encapsulation"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A33%3A50+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147039203225032135115025</font><a href="read2.asp?bookname=0672319942&snode=110&now=5%2F31%2F2002+4%3A33%3A50+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Polymorphism</h3>
				<p>The concept of polymorphism doesn't really apply to Python objects because Python doesn't offer type declaration. This concept (having a function or method work for multiple argument types) is something you get for free with Python because of the dynamic typing. It does exist, but you don't usually explicitly code for it. When handling an <tT CLAss="monofont">obj.method</tt> expression, the meaning of <TT CLass="monofont">method</tT> depends on the type, or <I>class,</I> of the object <Tt class="monofont">obj.</tt></p>

				<p>Python doesn't know what type of object implements an interface until the program is running. This feature is called <a name="idx1073743471"></a><a namE="idx1073743472"></a><i>runtime binding.</I></p>

				<p>Python <a Name="idx1073743473"></a><A namE="idx1073743474"></A>variables are typed, just not explicitly so. They are typed implicitly as the program uses them. For instance, if a program invokes <TT clasS="monofont">abs(x),</TT> it doesn't make sense for <Tt claSS="monofont">x</TT> to be any object but a number. Therefore, the variable <tt clASS="monofont">x</Tt> is informally typed.</p>

				<p>The capability of dealing with objects at different levels of abstraction is one of the most important features of object-oriented programming and a very important part of Python.</p>

				<p>The next example shows how you can use just one function to implement poly morphism in Python. C++ refers to this variety of polymorphism as <a name="idx1073743475"></a><a name="idx1073743476"></a><a naMe="idx1073743477"></a><I>method overloading.</i></p>

				<pRe>
					
&gt;&gt;&gt; class polymorph:
卍ef handle_int(self, argint):

⌨️ 快捷键说明

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