Single leading dash in member variable names?

Aahz aahz at pythoncraft.com
Sat Nov 10 16:41:19 CET 2012


In article <mailman.514.1347390405.27098.python-list at python.org>,
Ian Kelly  <ian.g.kelly at gmail.com> wrote:
>On Tue, Sep 11, 2012 at 12:45 PM,  <e.doxtator at 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 (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

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


More information about the Python-list mailing list