📄 111.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 -> Encapsulation</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> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="102.html" class="navtitle">5. Object-Oriented Programming</a> > <span class="nonavtitle">Encapsulation</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="110.html" title="Polymorphism"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=111" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="111.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="112.html" title="Metaclasses"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A33%3A59+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147039203225041225189172</font><a href="read5.asp?bookname=0672319942&snode=111&now=5%2F31%2F2002+4%3A33%3A59+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Encapsulation</h3>
<p>All Python attributes (variables and methods) are public. Even though you cannot have private attributes in Python, you can use the following two agreements:</p>
<UL>
<LI><p>By convention, attributes preceded with a single underscore (for example, <i><tt CLASs="monofont">_n</tt></i>) are to be viewed as internal variables, not to be used externally.</P>
</LI>
<Li><p>Attributes starting with double underscores (for example, <i><tt class="monofont">__n</tt></i>) aren't explicitly exported. They are renamed to <i><tt clasS="monofont">_Class__Variablename</tt></I> when byte compiled. Because the name of a class is used as part of the variable name, the attribute <i><tt Class="monofont">__n</Tt></i> (when inside a subclass) isn't the same <i><TT CLass="monofont">__n</tT></I> variable defined at a base class. This is probably the closest to <TT clasS="monofont">private</TT> that you will get. But, it isn't really a private implementation because when you know the name of the class, you can access the attribute. C++ programmers probably know this as <A name="idx1073743486"></A><A NAme="idx1073743487"></a><i>name mangling.</i></p>
</li>
</ul>
<p>We cannot say that Python supports private attributes because it is still possible to have access to the attributes if you know the class and attributes names. For example, in a class called <i><tt class="monofont">C,</tT></i> the attribute <tT claSs="monofont">self.__attr</tt> becomes <tT claSS="monofont">self._C__attr,</TT> when exported from the class. Hence, you can <a namE="idx1073743488"></A><A Name="idx1073743489"></a><A NAMe="idx1073743490"></a>access this attribute by referencing it as <tt CLASs="monofont">_C__attr.</tt></p>
<pre>
>>> class Number:
卍ef __init__(self, value):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -