Re: What's the difference between VAR and _VAR_?
On 8 Sep 2005 22:48:05 -0700 Johnny Lee [EMAIL PROTECTED] wrote: 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... It is just a programming style issue. In Python, variables and functions beginning with '_' are regarded by convention to be semi-private; essentially the Python equivalent of 'protected'. Since Python has no formal access specifications, conventions like this have been adopted. Likewise, '__' is private. Code using your code can bypass this, of course, but it's just a signal to the programmer 'avoid using this; it is an internal detail that may change.' Incidentally, epydoc regards all items beginning with '_' as private, and hides them from the public documentation it generates. And, in one project I'm working on, I use underscores to base/background/helper modules in a mechanism where modules are dynamically loaded (to prevent name collisions, since no dynamically loaded module will begin with an underscore). - Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
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).'' -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
I can't get the value of the variable (out of the class function) if it has two leading underscores. -Dave class encrypt: def encrypt(self, userValue): self.initialNumber = userValue self.__secretSeed = 7 return self.initialNumber * self.__secretSeed enc = encrypt() enc.encrypt(5) 35 print enc.initialNumber 5 print enc.__secretSeed Traceback (most recent call last): File pyshell#15, line 1, in -toplevel- print enc.__secretSeed AttributeError: encrypt instance has no attribute '__secretSeed' -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
S. D. Rose wrote: I can't get the value of the variable (out of the class function) if it has two leading underscores. Sure you can if you want it hard enough; it's just mangled. class C: __x = 1 ... dir(C) ['_C__x', '__doc__', '__module__'] c = C() dir(c) ['_C__x', '__doc__', '__module__'] c._C__x 1 -- Erik Max Francis [EMAIL PROTECTED] http://www.alcyone.com/max/ San Jose, CA, USA 37 20 N 121 53 W AIM erikmaxfrancis What is now prov'd was once only imagin'd. -- William Blake -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
As what you said, the following two code section is totally the same? (I) class TestResult: _passxxx_ = pass (II) class TestResult: passxxx = pass -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
Johnny Lee wrote: As what you said, the following two code section is totally the same? (I) class TestResult: _passxxx_ = pass (II) class TestResult: passxxx = pass No, of course not. One defines a class varaible named `_passxxx_', the other defines one named `passsxxx'. -- Erik Max Francis [EMAIL PROTECTED] http://www.alcyone.com/max/ San Jose, CA, USA 37 20 N 121 53 W AIM erikmaxfrancis Experience is the name everyone gives to their mistakes. -- Oscar Wilde -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
Erik Max Francis wrote: No, of course not. One defines a class varaible named `_passxxx_', the other defines one named `passsxxx'. I mean besides the difference of name... -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
Johnny Lee wrote: I mean besides the difference of name... You're going to have to be more clear; I don't understand your question. What's the difference between a = 1 and b = 1 besides the difference of name? -- Erik Max Francis [EMAIL PROTECTED] http://www.alcyone.com/max/ San Jose, CA, USA 37 20 N 121 53 W AIM erikmaxfrancis Who, my friend, can scale Heaven? -- _The Epic of Gilgamesh_ -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between VAR and _VAR_?
Erik Max Francis wrote: You're going to have to be more clear; I don't understand your question. What's the difference between a = 1 and b = 1 besides the difference of name? 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... -- http://mail.python.org/mailman/listinfo/python-list