What's the difference between VAR and _VAR_?

J

Johnny Lee

Hi,
I'm new in python and I was wondering what's the difference between
the two code section below:

(I)
class TestResult:
_pass_ = "pass"
_fail_ = "fail"
_exception_ = "exception"

(II)
class TestResult:
pass = "pass"
fail = "fail"
exception = "exception"

Thanks for your help.
 
E

Erik Max Francis

Johnny said:
I'm new in python and I was wondering what's the difference between
the two code section below:

(I)
class TestResult:
_pass_ = "pass"
_fail_ = "fail"
_exception_ = "exception"

(II)
class TestResult:
pass = "pass"
fail = "fail"
exception = "exception"

Thanks for your help.

There's nothing per se different between a variable named 'x' and one
named '_x_'. The difference here is that pass is a keyword, so

pass = 'pass'

is illegal.
 
J

Johnny Lee

As what you said, the following two code section is totally the same?

(I)
class TestResult:
_passxxx_ = "pass"

(II)
class TestResult:
passxxx = "pass"
 
E

Erik Max Francis

Johnny said:
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'.
 
J

Johnny Lee

Erik said:
No, of course not. One defines a class varaible named `_passxxx_', the
other defines one named `passsxxx'.


I mean.... besides the difference of name...
 
E

Erik Max Francis

Johnny said:
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?
 
J

Johnny Lee

Erik said:
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...
 
E

EP

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).''
 
S

S. D. Rose

I can't get the value of the variable (out of the class function) if it has
two leading underscores.

-Dave
def encrypt(self, userValue):
self.initialNumber = userValue
self.__secretSeed = 7
return self.initialNumber * self.__secretSeed

Traceback (most recent call last):
File "<pyshell#15>", line 1, in -toplevel-
print enc.__secretSeed
AttributeError: encrypt instance has no attribute '__secretSeed'
 
M

Michael Ekstrand

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,315
Members
48,001
Latest member
Wesley9486

Latest Threads

Top