What's the difference between VAR and _VAR_?

EP EP at zomething.com
Fri Sep 9 09:57:58 CEST 2005


> I thought there must be something special when you named a VAR with '_'
> the first character. Maybe it's just a programming style and I had
> thought too much...

Perhaps you are thinking of the use of double leading underscore names within class declarations or system defined names with underscores?  

e.g.  __somePrivateVar

e.g. __init__


"""
9.6 Private Variables 
There is limited support for class-private identifiers. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes. Truncation may occur when the mangled name would be longer than 255 characters. Outside classes, or when the class name consists of only underscores, no mangling occurs. 

Name mangling is intended to give classes an easy way to define ``private'' instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger, and that's one reason why this loophole is not closed. (Buglet: derivation of a class with the same name as the base class makes use of private variables of the base class possible.) 

Notice that code passed to exec, eval() or evalfile() does not consider the classname of the invoking class to be the current class; this is similar to the effect of the global statement, the effect of which is likewise restricted to code that is byte-compiled together. The same restriction applies to getattr(), setattr() and delattr(), as well as when referencing __dict__ directly. """



"""
2.3.2 Reserved classes of identifiers 
Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: 


_* 
Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the __builtin__ module. When not in interactive mode, "_" has no special meaning and is not defined. See section 6.12, ``The import statement.'' 
Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the gettext module for more information on this convention. 


__*__ 
System-defined names. These names are defined by the interpreter and it's implementation (including the standard library); applications should not expect to define additional names using this convention. The set of names of this class defined by Python may be extended in future versions. See section 3.3, ``Special method names.'' 

__* 
Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between ``private'' attributes of base and derived classes. See section 5.2.1, ``Identifiers (Names).'' 




--------------------------------------------------------------------------------

"""




More information about the Python-list mailing list