Question about idiomatic use of _ and private stuff.

Discussion in 'Python' started by Steven W. Orr, Feb 23, 2007.

  1. I understand that two leading underscores in a class attribute make the
    attribute private. But I often see things that are coded up with one
    underscore. Unless I'm missing something, there's a idiom going on here.

    Why do people sometimes use one leading underscore?

    TIA

    --
    Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
    happened but none stranger than this. Does your driver's license say Organ ..0
    Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    individuals! What if this weren't a hypothetical question?
    steveo at syslang.net
     
    Steven W. Orr, Feb 23, 2007
    #1
    1. Advertising

  2. Steven W. Orr

    goodwolf Guest

    On Feb 23, 5:12 pm, "Steven W. Orr" <> wrote:
    > I understand that two leading underscores in a class attribute make the
    > attribute private. But I often see things that are coded up with one
    > underscore. Unless I'm missing something, there's a idiom going on here.
    >
    > Why do people sometimes use one leading underscore?
    >
    > TIA
    >
    > --
    > Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
    > happened but none stranger than this. Does your driver's license say Organ ..0
    > Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    > individuals! What if this weren't a hypothetical question?
    > steveo at syslang.net


    One underscore stay for 'protected'. Protected in OOP means that the
    attribute is hidden outside the class but visible for subclasses. In
    python one undersore is only a good convention for say that the
    attribute is protected.
    So users will ignore attributes with initial undersocre. Users who
    subclass must know of that attributes existence for prevent unwonted
    overriding.

    Sory for my english.
     
    goodwolf, Feb 24, 2007
    #2
    1. Advertising

  3. Steven W. Orr a écrit :
    > I understand that two leading underscores in a class attribute make the
    > attribute private.


    Nope. It doesn't make it "private", it mangles the attribute name with
    the class name (ie : Bar.__mangled will become Bar._Bar__mangled
    everywhere except inside Bar). This is only useful when you want to make
    sure an attribute will not be *accidentally* accessed by a child class.
    FWIW, I've found it of very limited use so far...

    > But I often see things that are coded up with one
    > underscore. Unless I'm missing something, there's a idiom going on here.


    Yes. Single leading underscore means "this is implementation, don't mess
    with it or your on your own". It's the equivalent of "private".
     
    Bruno Desthuilliers, Feb 25, 2007
    #3
  4. Steven W. Orr

    Eric Brunel Guest

    On Sun, 25 Feb 2007 22:12:52 +0100, Bruno Desthuilliers
    <> wrote:

    > Steven W. Orr a écrit :
    >> I understand that two leading underscores in a class attribute make the
    >> attribute private.

    >
    > Nope. It doesn't make it "private", it mangles the attribute name with
    > the class name (ie : Bar.__mangled will become Bar._Bar__mangled
    > everywhere except inside Bar). This is only useful when you want to make
    > sure an attribute will not be *accidentally* accessed by a child class.
    > FWIW, I've found it of very limited use so far...


    If I'm not mistaken, it was originally introduced to allow designers of
    sub-classes to use any attribute name they liked, without bothering to go
    up the whole class hierarchy to make sure this name was not already used.
    Even if Python relies far less on inheritance than other languages, class
    hierarchies are sometimes quite large. If in addition, each class has a
    lot of attributes, looking for an unused name can become long and painful.
    In this context, the double-underscore may be a blessing.

    My [¤£$¥]0.02...
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in
    'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
     
    Eric Brunel, Feb 26, 2007
    #4
  5. Eric Brunel a écrit :
    > On Sun, 25 Feb 2007 22:12:52 +0100, Bruno Desthuilliers
    > <> wrote:
    >
    >> Steven W. Orr a écrit :
    >>
    >>> I understand that two leading underscores in a class attribute make
    >>> the attribute private.

    >>
    >>
    >> Nope. It doesn't make it "private", it mangles the attribute name
    >> with the class name (ie : Bar.__mangled will become
    >> Bar._Bar__mangled everywhere except inside Bar). This is only useful
    >> when you want to make sure an attribute will not be *accidentally*
    >> accessed by a child class. FWIW, I've found it of very limited use so
    >> far...

    >
    >
    > If I'm not mistaken, it was originally introduced to allow designers of
    > sub-classes to use any attribute name they liked, without bothering to
    > go up the whole class hierarchy to make sure this name was not already
    > used. > Even if Python relies far less on inheritance than other
    > languages, class hierarchies are sometimes quite large.


    Zope aside - but Zope is a world in itself, and certainly not the most
    Pythonic example of Python use -, I have not yet seen deep (I suppose
    that's what you mean here by "large") class hierarchies in Python.

    > If in addition,
    > each class has a lot of attributes, looking for an unused name can
    > become long and painful. In this context, the double-underscore may be
    > a blessing.


    My own experience is that it's often more trouble than gain. But please
    note that I said "of very limited use", not "totally useless" !-)

    > My [¤£$¥]0.02...


    <aol />
     
    Bruno Desthuilliers, Feb 27, 2007
    #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. qazmlp
    Replies:
    19
    Views:
    825
    Daniel T.
    Feb 4, 2004
  2. Jeremy Bowers
    Replies:
    3
    Views:
    555
    Steven Bethard
    Jan 24, 2005
  3. Troy Melhase
    Replies:
    3
    Views:
    257
    Bruno Desthuilliers
    Feb 25, 2007
  4. Steven W. Orr
    Replies:
    2
    Views:
    260
    goodwolf
    Feb 24, 2007
  5. jaems

    some stuff private

    jaems, Mar 19, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    274
    Cowboy \(Gregory A. Beamer\)
    Mar 19, 2008
Loading...

Share This Page