Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Single leading dash in member variable names?

Reply
Thread Tools

Single leading dash in member variable names?

 
 
e.doxtator@gmail.com
Guest
Posts: n/a
 
      09-11-2012
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.

 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      09-11-2012
On Tue, Sep 11, 2012 at 12:45 PM, <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
e.doxtator@gmail.com
Guest
Posts: n/a
 
      09-11-2012
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?
 
Reply With Quote
 
e.doxtator@gmail.com
Guest
Posts: n/a
 
      09-11-2012
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?
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-11-2012
On 9/11/2012 4:53 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-12-2012
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


 
Reply With Quote
 
e.doxtator@gmail.com
Guest
Posts: n/a
 
      09-12-2012
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/...ut.html#naming)
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      09-12-2012
On Tue, Sep 11, 2012 at 11:10 PM, Dwight Hutto <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      09-12-2012
On Wed, Sep 12, 2012 at 9:23 AM, <(E-Mail Removed)> 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/...ut.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.
 
Reply With Quote
 
mdengler@gmail.com
Guest
Posts: n/a
 
      09-12-2012
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/...ut.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".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Option parsers that support single-dash words? Jos Backus Ruby 4 06-12-2010 03:03 PM
n-dash & m-dash display in Opera, FF Rigour HTML 4 11-17-2009 07:41 AM
All leading tabs or all leading spaces - why isn't that enforced? John Nagle Python 4 08-07-2007 04:05 PM
RE: All leading tabs or all leading spaces - why isn't that enforced? Delaney, Timothy (Tim) Python 0 08-07-2007 03:50 AM
Dash in SOAP variable name lukas Shb Ruby 4 07-31-2007 01:45 PM



Advertisments