Re: To make a method or attribute private

Discussion in 'Python' started by iMath, Jan 21, 2013.

  1. iMath

    iMath Guest

    在 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 ?
     
    iMath, Jan 21, 2013
    #1
    1. Advertising

  2. On Mon, Jan 21, 2013 at 12:14 PM, iMath <> 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
     
    Chris Angelico, Jan 21, 2013
    #2
    1. Advertising

  3. 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
     
    Steven D'Aprano, Jan 21, 2013
    #3
  4. On Mon, Jan 21, 2013 at 2:27 PM, Steven D'Aprano
    <> 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
     
    Chris Angelico, Jan 21, 2013
    #4
  5. iMath

    alex23 Guest

    On Jan 21, 2:46 pm, Chris Angelico <> 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...
     
    alex23, Jan 21, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daz
    Replies:
    3
    Views:
    110
    Matt Kruse
    May 11, 2007
  2. alex23
    Replies:
    2
    Views:
    168
    Steven D'Aprano
    Jan 20, 2013
  3. Steven D'Aprano

    Re: To make a method or attribute private

    Steven D'Aprano, Jan 17, 2013, in forum: Python
    Replies:
    0
    Views:
    139
    Steven D'Aprano
    Jan 17, 2013
  4. Lie Ryan
    Replies:
    0
    Views:
    115
    Lie Ryan
    Jan 18, 2013
  5. iMath
    Replies:
    5
    Views:
    142
    Mitya Sirenef
    Jan 21, 2013
Loading...

Share This Page