Question about idiomatic use of _ and private stuff.

Chris Mellon arkanes at gmail.com
Fri Feb 23 17:40:59 EST 2007


On 2/23/07, James Stroud <jstroud at mbi.ucla.edu> wrote:
> Troy Melhase wrote:
> >> Why do people sometimes use one leading underscore?
> >
> >
> > Many folks like to use the single leading underscore to emphasize that
> > the attribute isn't part of the normal way to use the class or
> > instance.
> >
> > It's bad style in my opinion, but I'm probably in the minority.
>
> I've increasingly found that leading underscores are unnecessary as well
> if not using a "magic" attribute with the bounding double underscores.
> --

I use the single underscore a lot and should use it more. It indicates
something that you shouldn't look at unless you understand and are
willing to bind yourself to the class internals.

For example, I have a network interface that buffers data as it's
being parsed. The internal buffer is a list named _buffer, and messing
with the buffer is a good way to break the parser.

There's an exterior interface, which is a guaranteed consistent buffer
called "message", but (even for me, the author) it's easy to get
confused about which one is the safe, public attribute and which one
is private. The underscore disambiguates.



More information about the Python-list mailing list