To make a method or attribute private

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

  1. iMath

    iMath Guest

    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 methodsor attributes?
    iMath, Jan 20, 2013
    #1
    1. Advertising

  2. Chris Angelico, Jan 20, 2013
    #2
    1. Advertising

  3. iMath

    alex23 Guest

    On Jan 20, 7:23 pm, Chris Angelico <> wrote:
    > On Sun, Jan 20, 2013 at 8:17 PM, iMath <> wrote:
    > > so what is your opinion about single leading underscore and private methods or attributes?

    >
    > Didn't this get discussed recently?
    >
    > http://mail.python.org/pipermail/python-list/2013-January/638687.html
    >
    > ChrisA


    Isn't that a link to the same post that started this thread? :)
    alex23, Jan 20, 2013
    #3
  4. iMath

    Dave Angel Guest

    On 01/20/2013 06:14 PM, alex23 wrote:
    > On Jan 20, 7:23 pm, Chris Angelico <> wrote:
    >> On Sun, Jan 20, 2013 at 8:17 PM, iMath <> wrote:
    >>> so what is your opinion about single leading underscore and private methods or attributes?

    >>
    >> Didn't this get discussed recently?
    >>
    >> http://mail.python.org/pipermail/python-list/2013-January/638687.html
    >>
    >> ChrisA

    >
    > Isn't that a link to the same post that started this thread? :)
    >


    No, that's the one that started the earlier thread, by the same name,
    three whole days ago. iMath posted an apparently complete duplicate of
    his earlier message.


    --
    DaveA
    Dave Angel, Jan 20, 2013
    #4
  5. iMath

    alex23 Guest

    On Jan 21, 9:32 am, Dave Angel <> wrote:
    > On 01/20/2013 06:14 PM, alex23 wrote:
    >
    > > On Jan 20, 7:23 pm, Chris Angelico <> wrote:
    > >> On Sun, Jan 20, 2013 at 8:17 PM, iMath <> wrote:
    > >>> so what is your opinion about single leading underscore and private methods or attributes?

    >
    > >> Didn't this get discussed recently?

    >
    > >>http://mail.python.org/pipermail/python-list/2013-January/638687.html

    >
    > >> ChrisA

    >
    > > Isn't that a link to the same post that started this thread? :)

    >
    > No, that's the one that started the earlier thread, by the same name,
    > three whole days ago.  iMath posted an apparently complete duplicateof
    > his earlier message.


    The link is to a post of the same date as the original of this thread,
    and the very first response is mine, same as with this thread. I'm
    still not seeing the dupe?
    alex23, Jan 21, 2013
    #5
  6. On 01/20/2013 09:24 PM, alex23 wrote:
    > On Jan 21, 9:32 am, Dave Angel <> wrote:
    >> On 01/20/2013 06:14 PM, alex23 wrote:
    >>
    >>> On Jan 20, 7:23 pm, Chris Angelico <> wrote:
    >>>> On Sun, Jan 20, 2013 at 8:17 PM, iMath <> wrote:
    >>>>> so what is your opinion about single leading underscore and

    private methods or attributes?
    >>
    >>>> Didn't this get discussed recently?

    >>
    >>>> http://mail.python.org/pipermail/python-list/2013-January/638687.html

    >>
    >>>> ChrisA

    >>
    >>> Isn't that a link to the same post that started this thread? :)

    >>
    >> No, that's the one that started the earlier thread, by the same name,
    >> three whole days ago. iMath posted an apparently complete duplicate of
    >> his earlier message.

    >
    > The link is to a post of the same date as the original of this thread,
    > and the very first response is mine, same as with this thread. I'm
    > still not seeing the dupe?


    I do see the duplicate in my reader.. -m


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    To knock a thing down, especially if it is cocked at an arrogant angle, is
    a deep delight of the blood.
    George Santayana
    Mitya Sirenef, Jan 21, 2013
    #6
    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:
    93
    Matt Kruse
    May 11, 2007
  2. alex23
    Replies:
    2
    Views:
    152
    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:
    121
    Steven D'Aprano
    Jan 17, 2013
  4. Lie Ryan
    Replies:
    0
    Views:
    107
    Lie Ryan
    Jan 18, 2013
  5. iMath
    Replies:
    4
    Views:
    120
    alex23
    Jan 21, 2013
Loading...

Share This Page