![]() |
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. |
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. |
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? |
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? |
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 |
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 |
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) |
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. |
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. |
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.