On Sat, Jan 12, 2002 at 01:07:33PM +0100, Frank Barknecht wrote:
> Tim Payne hat gesagt: // Tim Payne wrote:
> > I was also wondering why so many of the variables are named like _name
> > or _password? I've seen this sort of naming convention a lot in python,
> > but I'm unsure of what cases this is used in.
> These are a kind of "private" variables. Names starting with an _underscore
> don't get imported if you do an "from foo import *"
Python has two levels of private variables. _variable works as above.
Not only are they ignored by "from foo import *", but they are a signal
to users of the class, "Don't use me directly." Sometimes you have to
anyway, but those are borderline cases.
The other level of private variable, __classAttributeOrMethod, works only
inside a class definition. When the module is compiled, "__" is
converted to "className_". This prevents subclasses, users and mixin
classes from accidentally overriding them with same-name attributes that
are used for a different purpose.
Variables/attributes with an underscore at both ends; e.g., __name__,
are not private. Instead, Python's predefined variables use this
scheme to indicate the variable has some special effect.
WebKit (but I don't know about UserKit) has sets of similarly-named
attributes and methods. In each set, _variable is private.
variable() is used to read the value, setVariable(newValue) to
set it. For multi-value variables (dictionary-like), variables()
returns a read-only dictionary of all values; e.g., fields().
There has been discussion about using Python 2.2's new "properties"
feature to combine _variable, variable() and setVariable() into one
property or "smart attribute" called simply variable, but no decision
has been made yet.
-Mike (Iron) Orr, iron@... (if mail problems: mso@...)
http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol