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

 
 
iMath
Guest
Posts: n/a
 
      01-21-2013
在 2013年1月17日星期四UTC+8上午8时34分22秒 ,iMath写道:
> 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 .
>
>
> >>> class A:

> ... * * def __init__(self):
> ... * * * * self.a = 'a'
> ... * * * * self._a = '_a'
> ... * * * * self.__a = '__a'
> ...
>
>
>
>
>
>
> >>> ap = A()
> >>> ap.a

> 'a'
> >>> ap._a

> '_a'
> >>> ap.__a

> Traceback (most recent call last):
> * File "<stdin>", line 1, in ?
> AttributeError: A instance has no attribute '__a'
>
>
> so what is your opinion about single leading underscore and private methods or attributes?


so there is no REAL private variable in Python but conversion exists in it that python programmer should follow and recognize .right ?
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-21-2013
On Mon, Jan 21, 2013 at 12:14 PM, iMath <(E-Mail Removed)> wrote:
> so there is no REAL private variable in Python but conversion exists in it that python programmer should follow and recognize .right ?


That's about it. If you think about C++ public members as the
"interface" and private/protected members as the "implementation",
then Python's convention is a leading underscore on the latter; you
can reasonably expect that non-underscore members can be trusted to be
maintained, but underscored members will quite possibly change in
subsequent versions.

Among smart people, conventions like this are all you need.

ChrisA
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-21-2013
On Sun, 20 Jan 2013 17:14:36 -0800, iMath wrote:

[...]
> so there is no REAL private variable in Python but conversion exists in
> it that python programmer should follow and recognize .right ?


There are no "REAL private variables" in most languages. Consider the C++
trick "#define private public". Or pointer tricks, or using reflection in
Java.

Yes, the convention in Python is that names starting with a single
underscore should be considered private implementation details.



--
Steven
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-21-2013
On Mon, Jan 21, 2013 at 2:27 PM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> On Sun, 20 Jan 2013 17:14:36 -0800, iMath wrote:
>
> [...]
>> so there is no REAL private variable in Python but conversion exists in
>> it that python programmer should follow and recognize .right ?

>
> There are no "REAL private variables" in most languages. Consider the C++
> trick "#define private public". Or pointer tricks, or using reflection in
> Java.


Uhh, that's like saying there are no REAL floats in C, because you can
#define float int
And pointer tricks, well, you can do anything with raw memory access.
These aren't proofs that something doesn't exist, they're proofs that
trying to enforce privacy is bound to fail - so you may as well strip
that code from your compiler/interpreter and go with the Python style.
Much easier.

I agree with your point, just not your argument.

ChrisA
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      01-21-2013
On Jan 21, 2:46*pm, Chris Angelico <(E-Mail Removed)> wrote:
> These aren't proofs that something doesn't exist, they're proofs that
> trying to enforce privacy is bound to fail


But if you can't enforce it, can you really say it exists?

Semantics, they are fun! I feel another PyWart post coming on...
 
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
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 Lie Ryan Python 0 01-18-2013 07:46 AM
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