Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Single leading dash in member variable names? (http://www.velocityreviews.com/forums/t952070-single-leading-dash-in-member-variable-names.html)

e.doxtator@gmail.com 09-11-2012 06:45 PM

Single leading dash in member variable names?
 
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 09-11-2012 07:06 PM

Re: Single leading dash in member variable names?
 
On Tue, Sep 11, 2012 at 12:45 PM, <e.doxtator@gmail.com> 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.

e.doxtator@gmail.com 09-11-2012 08:53 PM

Re: Single leading dash in member variable names?
 
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?

e.doxtator@gmail.com 09-11-2012 08:53 PM

Re: Single leading dash in member variable names?
 
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?

Terry Reedy 09-11-2012 09:34 PM

Re: Single leading dash in member variable names?
 
On 9/11/2012 4:53 PM, e.doxtator@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


Tim Chase 09-12-2012 10:49 AM

Re: Single leading dash in member variable names?
 
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.doxtator@gmail.com 09-12-2012 03:23 PM

Re: Single leading dash in member variable names?
 
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)

Ian Kelly 09-12-2012 03:23 PM

Re: Single leading dash in member variable names?
 
On Tue, Sep 11, 2012 at 11:10 PM, Dwight Hutto <dwightdhutto@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 09-12-2012 04:52 PM

Re: Single leading dash in member variable names?
 
On Wed, Sep 12, 2012 at 9:23 AM, <e.doxtator@gmail.com> 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.

mdengler@gmail.com 09-12-2012 04:53 PM

Re: Single leading dash in member variable names?
 
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".


All times are GMT. The time now is 06:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57