Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36922

To make a method or attribute private

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <2281570025@qq.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.039
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'attribute': 0.05; 'attributes': 0.07; 'python': 0.09; 'indicates': 0.09; 'subject:method': 0.09; 'underscore': 0.09; 'def': 0.10; 'underscores': 0.16; 'instance': 0.17; 'saying': 0.18; 'variable': 0.20; 'to:name:python-list@python.org': 0.20; '&gt;&gt;&gt;': 0.22; 'seems': 0.23; '(most': 0.27; 'rules': 0.27; 'skip:_ 10': 0.29; 'class': 0.29; 'skip:? 10': 0.30; 'file': 0.32; 'skip:- 10': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'there': 0.35; 'but': 0.36; 'method': 0.36; 'test': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'your': 0.60; 'leading': 0.61; 'real': 0.61; '8bit%:20': 0.62; '8bit%:43': 0.65; 'skip:\xe2 10': 0.66; 'from:addr:qq.com': 0.84; 'message- id:@qq.com': 0.84; 'received:qq.com': 0.84; 'x-mailer:qqmail 2.x': 0.84; 'novice': 0.91
From "iMath" <2281570025@qq.com>
To "python-list@python.org" <python-list@python.org>
Subject To make a method or attribute private
Mime-Version 1.0
Content-Type text/html; charset="utf-8"
Content-Transfer-Encoding base64
Date Thu, 17 Jan 2013 08:34:22 +0800
X-Priority 3
X-QQ-MIME TCMime 1.0 by Tencent
X-Mailer QQMail 2.x
X-QQ-Mailer QQMail 2.x
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.584.1358382936.2939.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358382936 news.xs4all.nl 6950 [2001:888:2000:d::a6]:56304
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36922

Show key headers only | View raw


<div><div>To make a method or attribute private (inaccessible from the outside), simply start its&nbsp;</div><div>name with two underscores</div><div><br></div><div>----《Beginning Python From Novice to Professional》</div><div><br></div><div>but there is another saying goes:</div><div>Beginning a variable name with a single underscore indicates that the variable should be treated as ‘private’.</div><div><br></div><div>I test both these 2 rules ,it seems only names that start with two underscores are REAL private methods or attributes .</div><div><br></div><div>&gt;&gt;&gt; class A:</div><div>... &nbsp; &nbsp; def __init__(self):</div><div>... &nbsp; &nbsp; &nbsp; &nbsp; self.a = 'a'</div><div>... &nbsp; &nbsp; &nbsp; &nbsp; self._a = '_a'</div><div>... &nbsp; &nbsp; &nbsp; &nbsp; self.__a = '__a'</div><div>...</div><div><br></div><div><br></div><div><br></div><div>&gt;&gt;&gt; ap = A()</div><div>&gt;&gt;&gt; ap.a</div><div>'a'</div><div>&gt;&gt;&gt; ap._a</div><div>'_a'</div><div>&gt;&gt;&gt; ap.__a</div><div>Traceback (most recent call last):</div><div>&nbsp; File "&lt;stdin&gt;", line 1, in ?</div><div>AttributeError: A instance has no attribute '__a'</div><div><br></div><div>so what is your opinion about single leading underscore and private methods or attributes?</div></div>

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

To make a method or attribute private  "iMath" <2281570025@qq.com> - 2013-01-17 08:34 +0800
  Re: To make a method or attribute private alex23 <wuwei23@gmail.com> - 2013-01-16 17:04 -0800
    Re: To make a method or attribute private iMath <redstone-cold@163.com> - 2013-01-20 06:52 -0800
      Re: To make a method or attribute private Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-20 15:15 +0000
  Re: To make a method or attribute private Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-17 02:38 +0000
  Re: To make a method or attribute private iMath <redstone-cold@163.com> - 2013-01-20 17:14 -0800
    Re: To make a method or attribute private Chris Angelico <rosuav@gmail.com> - 2013-01-21 12:26 +1100
    Re: To make a method or attribute private Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-21 03:27 +0000
      Re: To make a method or attribute private Chris Angelico <rosuav@gmail.com> - 2013-01-21 15:46 +1100
        Re: To make a method or attribute private alex23 <wuwei23@gmail.com> - 2013-01-20 21:44 -0800

csiph-web