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


Groups > comp.lang.python > #36978

Re: To make a method or attribute private

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'attributes': 0.07; 'convention.': 0.07; 'python': 0.09; 'indicates': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'underscore': 0.09; 'clashes': 0.16; 'inaccessible': 0.16; 'mangled': 0.16; 'naming': 0.16; 'pointers.': 0.16; 'received:80.91.229.3': 0.16; 'received:edu.au': 0.16; 'received:plane.gmane.org': 0.16; 'subclasses,': 0.16; 'underscores': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'saying': 0.18; '>>>': 0.18; 'variable': 0.20; 'meant': 0.21; 'programming': 0.23; 'seems': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'rules': 0.27; 'header:X-Complaints-To:1': 0.28; 'trigger': 0.29; "we're": 0.30; 'usually': 0.30; 'generally': 0.32; 'skip:- 10': 0.32; 'accessible': 0.33; 'to:addr:python-list': 0.33; 'languages': 0.33; 'another': 0.33; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'received:au': 0.36; 'totally': 0.36; 'method': 0.36; 'test': 0.36; 'should': 0.36; 'does': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'easy': 0.60; 'most': 0.61; 'leading': 0.61; 'high': 0.61; 'real': 0.61; '8bit%:20': 0.62; 'making': 0.64; '8bit%:43': 0.65; 'risk': 0.66; 'skip:\xe2 10': 0.66; 'novice': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Lie Ryan <lie.1296@gmail.com>
Subject Re: To make a method or attribute private
Date Fri, 18 Jan 2013 18:46:55 +1100
References <tencent_4996C2EC57A77C1830541D13@qq.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host cpmon.mq.edu.au
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
In-Reply-To <tencent_4996C2EC57A77C1830541D13@qq.com>
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.613.1358495230.2939.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358495230 news.xs4all.nl 6976 [2001:888:2000:d::a6]:51262
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36978

Show key headers only | View raw


On 17/01/13 11:34, iMath wrote:
> To make a method or attribute private (inaccessible from the outside),
> simply start its name with two underscores
>
> ----《Beginning Python From Novice to Professional》
>
> but there is another saying goes:
> Beginning a variable name with a single underscore indicates that the
> variable should be treated as ‘private’.
>
> I test both these 2 rules ,it seems only names that start with two
> underscores are REAL private methods or attributes .

Python does not have a REAL private methods/attributes. The double 
leading underscore is meant to trigger name mangling to avoid naming 
collisions with subclasses, the method/attribute is still accessible 
using the mangled name:

 >>> ap._A__a
'__a'

You generally only use double leading underscores when your private 
method/attribute is in a very high risk of having naming clashes with 
superclasses/subclasses.

 > so what is your opinion about single leading underscore and private
 > methods or attributes?

We're all consenting adults. Use methods/attributes with single or 
double leading underscore at your own risk.

Most programming languages do not actually have a private attribute that 
is totally inaccessible from outside, there are usually ways work around 
access restrictions, usually using reflections or pointers. Python only 
makes it easy to do so by making private variables only a convention.

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


Thread

Re: To make a method or attribute private Lie Ryan <lie.1296@gmail.com> - 2013-01-18 18:46 +1100

csiph-web