Single leading dash in member variable names?

Discussion in 'Python' started by e.doxtator@gmail.com, Sep 11, 2012.

  1. Guest

    All

    Python noob here. Trying to understand a particular syntax:

    class stuff:
    def __init__(self):
    self._bongo = "BongoWorld"

    -----------

    What is the significance of the leading underscore in "self._bongo"? I've seen this a few times and, after looking through PEP 8, I didn't see anything relevant, but I could have missed it.
     
    , Sep 11, 2012
    #1
    1. Advertising

  2. Ian Kelly Guest

    On Tue, Sep 11, 2012 at 12:45 PM, <> wrote:
    > All
    >
    > Python noob here. Trying to understand a particular syntax:
    >
    > class stuff:
    > def __init__(self):
    > self._bongo = "BongoWorld"
    >
    > -----------
    >
    > What is the significance of the leading underscore in "self._bongo"? I've seen this a few times and, after looking through PEP 8, I didn't see anything relevant, but I could have missed it.


    Single leading underscore is a convention indicating that the name
    should be considered private and not used externally. It's a softer
    version of the double leading underscore that means basically the same
    thing but has syntactic significance.
     
    Ian Kelly, Sep 11, 2012
    #2
    1. Advertising

  3. Guest

    On Tuesday, September 11, 2012 2:06:45 PM UTC-5, Ian wrote:
    > On Tue, Sep 11, 2012 at 12:45 PM, I wrote:
    >
    > > All

    >
    > >

    >
    > > Python noob here. Trying to understand a particular syntax:

    >
    > >

    >
    > > class stuff:

    >
    > > def __init__(self):

    >
    > > self._bongo = "BongoWorld"

    >
    > >

    >
    > > -----------

    >
    > >

    >
    > > What is the significance of the leading underscore in "self._bongo"? I've seen this a few times and, after looking through PEP 8, I didn't see anything relevant, but I could have missed it.

    >
    >
    >
    > Single leading underscore is a convention indicating that the name
    >
    > should be considered private and not used externally. It's a softer
    >
    > version of the double leading underscore that means basically the same
    >
    > thing but has syntactic significance.


    Thank you!

    PEP 8 says this is bad form. What do you think?
     
    , Sep 11, 2012
    #3
  4. Guest

    On Tuesday, September 11, 2012 2:06:45 PM UTC-5, Ian wrote:
    > On Tue, Sep 11, 2012 at 12:45 PM, I wrote:
    >
    > > All

    >
    > >

    >
    > > Python noob here. Trying to understand a particular syntax:

    >
    > >

    >
    > > class stuff:

    >
    > > def __init__(self):

    >
    > > self._bongo = "BongoWorld"

    >
    > >

    >
    > > -----------

    >
    > >

    >
    > > What is the significance of the leading underscore in "self._bongo"? I've seen this a few times and, after looking through PEP 8, I didn't see anything relevant, but I could have missed it.

    >
    >
    >
    > Single leading underscore is a convention indicating that the name
    >
    > should be considered private and not used externally. It's a softer
    >
    > version of the double leading underscore that means basically the same
    >
    > thing but has syntactic significance.


    Thank you!

    PEP 8 says this is bad form. What do you think?
     
    , Sep 11, 2012
    #4
  5. Terry Reedy Guest

    On 9/11/2012 4:53 PM, wrote:

    >>> What is the significance of the leading underscore in "self._bongo"? I've seen this a few times and, after looking through PEP 8, I didn't see anything relevant, but I could have missed it.


    >> Single leading underscore is a convention indicating that the name
    >> should be considered private and not used externally. It's a softer
    >> version of the double leading underscore that means basically the same
    >> thing but has syntactic significance.


    > PEP 8 says this is bad form. What do you think?


    Please quote the specific statement you want commented. The stdlib
    routinely uses _names for internal implementation objects. __ugh is
    perhaps never used.

    --
    Terry Jan Reedy
     
    Terry Reedy, Sep 11, 2012
    #5
  6. Tim Chase Guest

    On 09/12/12 00:10, Dwight Hutto wrote:
    > Not to jump in with another question(this seems somewhat relevant
    > to the conversation, maybe not), but is this similar to a
    > private,public, or protected class similar to the C type langs?


    Close, but C-like languages tend to strictly enforce it, while in
    Python it's more of a gentleman's agreement. Python doesn't *stop*
    you from mucking with them, but you've been advised that, if it
    breaks, you get to keep both parts.

    -tkc
     
    Tim Chase, Sep 12, 2012
    #6
  7. Guest

    On Tuesday, September 11, 2012 5:02:31 PM UTC-5, Erik Max Francis wrote:
    > On 09/11/2012 01:53 PM, me wrote:
    >
    > > On Tuesday, September 11, 2012 2:06:45 PM UTC-5, Ian wrote:

    >
    > >> On Tue, Sep 11, 2012 at 12:45 PM, I wrote:

    >
    > >>> What is the significance of the leading underscore in "self._bongo"? I've seen this a few times and, after looking through PEP 8, I didn't see anything relevant, but I could have missed it.

    >
    > >>

    >
    > >> Single leading underscore is a convention indicating that the name

    >
    > >> should be considered private and not used externally. It's a softer

    >
    > >> version of the double leading underscore that means basically the same

    >
    > >> thing but has syntactic significance.

    >
    > >

    >
    > > Thank you!

    >
    > >

    >
    > > PEP 8 says this is bad form. What do you think?

    >
    >
    >
    > Where does it say that?


    Apologies. It's in David Goodger's "Code Like A Pythonista" in the "Naming" section. (http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming)
     
    , Sep 12, 2012
    #7
  8. Ian Kelly Guest

    On Tue, Sep 11, 2012 at 11:10 PM, Dwight Hutto <> wrote:
    > Not to jump in with another question(this seems somewhat relevant to the
    > conversation, maybe not), but is this similar to a private,public, or
    > protected class similar to the C type langs?


    More like "this is an implementation detail and in the future it could
    be changed or removed entirely without warning". I consider them
    private unless documented otherwise.
     
    Ian Kelly, Sep 12, 2012
    #8
  9. Ian Kelly Guest

    On Wed, Sep 12, 2012 at 9:23 AM, <> wrote:
    >
    > On Tuesday, September 11, 2012 5:02:31 PM UTC-5, Erik Max Francis wrote:
    > > On 09/11/2012 01:53 PM, me wrote:
    > > > PEP 8 says this is bad form. What do you think?

    > >
    > >
    > >
    > > Where does it say that?

    >
    > Apologies. It's in David Goodger's "Code Like A Pythonista" in the "Naming" section. (http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming)


    That's arguing against double leading underscore, not single leading
    underscore. I pretty much agree with it; I rarely use the
    name-mangling syntax myself.
     
    Ian Kelly, Sep 12, 2012
    #9
  10. Guest

    On Wednesday, September 12, 2012 4:23:49 PM UTC+1, (unknown) wrote:
    > [...] David Goodger's "Code Like A Pythonista" in the "Naming" section [says single leading underscore is bad form]. (http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming)


    Looks like it says the opposite:

    "[rather than trying to hide attributes with double-leading-underscores, i]t's better to use the single-leading-underscore convention, _internal".
     
    , Sep 12, 2012
    #10
  11. Aahz Guest

    In article <>,
    Ian Kelly <> wrote:
    >On Tue, Sep 11, 2012 at 12:45 PM, <> wrote:
    >>
    >> Python noob here. Trying to understand a particular syntax:
    >>
    >> class stuff:
    >> def __init__(self):
    >> self._bongo = "BongoWorld"
    >>
    >> What is the significance of the leading underscore in "self._bongo"?
    >> I've seen this a few times and, after looking through PEP 8, I didn't
    >> see anything relevant, but I could have missed it.

    >
    >Single leading underscore is a convention indicating that the name
    >should be considered private and not used externally. It's a softer
    >version of the double leading underscore that means basically the same
    >thing but has syntactic significance.


    Note that the convention is rooted in an actual semantic meaning for
    single underscore:

    ``from foo import *`` ignores any module global names in foo that start
    with a single leading underscore. Obviously, this has little effect for
    most Python programs because you DON'T USE ``import *``.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "....Normal is what cuts off your sixth finger and your tail..." --Siobhan
     
    Aahz, Nov 10, 2012
    #11
    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. Walter Tross

    leading underscore for member names

    Walter Tross, Apr 29, 2004, in forum: C++
    Replies:
    5
    Views:
    555
    Sumit Rajan
    Apr 29, 2004
  2. John Nagle
    Replies:
    4
    Views:
    606
    James Antill
    Aug 7, 2007
  3. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    573
    Delaney, Timothy (Tim)
    Aug 7, 2007
  4. Rigour
    Replies:
    4
    Views:
    1,428
    Neredbojias
    Nov 17, 2009
  5. Jos Backus
    Replies:
    4
    Views:
    131
    Robert Klemme
    Jun 12, 2010
Loading...

Share This Page