Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: To make a method or attribute private

Reply
Thread Tools

Re: To make a method or attribute private

 
 
Lie Ryan
Guest
Posts: n/a
 
      01-18-2013
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: To make a method or attribute private iMath Python 4 01-21-2013 05:44 AM
To make a method or attribute private iMath Python 5 01-21-2013 02:45 AM
Re: To make a method or attribute private alex23 Python 2 01-20-2013 03:15 PM
Re: To make a method or attribute private Steven D'Aprano Python 0 01-17-2013 02:38 AM
How can I make XMLHttpRequsts onreadystatechange a private method? Daz Javascript 3 05-11-2007 09:48 PM



Advertisments