[Poll] Private variables
tim.one at home.com
Wed Sep 12 22:13:42 CEST 2001
> It is possible in python to get private class members by using names
> beginning with 2 underscores, and finished by at least 1 underscore.
You don't need an underscore at the end. A name in a class is private if
and only if it begins with at least two underscores and does not end with at
least two underscores (names that both begin and end with at least two
underscores are reserved -- like __class__ and __add__ and __init__ and even
__). So __private and __private_ are both private names, but Python
reserves the right to take __private__ away for its own purposes.
> I personnally tend to use class member names beginning with one
> underscore to indicate that the member is 'internal' and should not
> be called by external applications, and from looking at the standard
> library source code, it seems that this is more common than the official
> way mentionned hereabove.
They serve different purposes -- a single leading underscore is merely
advisory, while a private name is also merely advisory <wink> but indicates
you're more serious about it. Semantically, the single leading underscore
means nothing, so is purely convention. Private names have a tiny bit of
(easily circumvented, but hard to *accidentally* circumvent) enforcement.
Most of the time Python class authors aren't interested in interfering with
how people want to use their classes, but any leading underscore screams
"use at your own risk".
as-if-you-ever-used-anything-at-anyone-else's-risk<wink>-ly y'rs - tim
More information about the Python-list