Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Single leading dash in member variable names?

58 views
Skip to first unread message

e.dox...@gmail.com

unread,
Sep 11, 2012, 2:45:25 PM9/11/12
to
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.

Ian Kelly

unread,
Sep 11, 2012, 3:06:06 PM9/11/12
to Python
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.

e.dox...@gmail.com

unread,
Sep 11, 2012, 4:53:26 PM9/11/12
to Python
On Tuesday, September 11, 2012 2:06:45 PM UTC-5, Ian wrote:
Thank you!

PEP 8 says this is bad form. What do you think?
Message has been deleted

Terry Reedy

unread,
Sep 11, 2012, 5:34:03 PM9/11/12
to pytho...@python.org
On 9/11/2012 4:53 PM, e.dox...@gmail.com 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

Erik Max Francis

unread,
Sep 11, 2012, 6:02:31 PM9/11/12
to
On 09/11/2012 01:53 PM, e.dox...@gmail.com 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?

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Jabber erikmaxfrancis
I will always remember / This moment
-- Sade

Tim Chase

unread,
Sep 12, 2012, 6:49:46 AM9/12/12
to Dwight Hutto, Erik Max Francis, pytho...@python.org
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


e.dox...@gmail.com

unread,
Sep 12, 2012, 11:23:49 AM9/12/12
to
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)

Ian Kelly

unread,
Sep 12, 2012, 11:23:58 AM9/12/12
to pytho...@python.org
On Tue, Sep 11, 2012 at 11:10 PM, Dwight Hutto <dwight...@gmail.com> 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

unread,
Sep 12, 2012, 12:52:21 PM9/12/12
to Python
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.

mden...@gmail.com

unread,
Sep 12, 2012, 12:53:07 PM9/12/12
to
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".

Aahz

unread,
Nov 10, 2012, 10:41:19 AM11/10/12
to
In article <mailman.514.13473904...@python.org>,
Ian Kelly <ian.g...@gmail.com> wrote:
>On Tue, Sep 11, 2012 at 12:45 PM, <e.dox...@gmail.com> 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 (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"....Normal is what cuts off your sixth finger and your tail..." --Siobhan
0 new messages