By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

What's the difference between VAR and _VAR_?

P: n/a
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.

Sep 9 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Johnny Lee wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && 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
Sep 9 '05 #2

P: n/a
As what you said, the following two code section is totally the same?

(I)
class TestResult:
_passxxx_ = "pass"

(II)
class TestResult:
passxxx = "pass"

Sep 9 '05 #3

P: n/a
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 && ma*@alcyone.com && 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
Sep 9 '05 #4

P: n/a

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...

Sep 9 '05 #5

P: n/a
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 && ma*@alcyone.com && 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_
Sep 9 '05 #6

P: n/a

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...

Sep 9 '05 #7

P: n/a
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 thecurrent 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 theclass 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 asthe base class makes use of private variables of the base class possible.)

Notice that code passed to exec, eval() or evalfile() does not consider theclassname 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 section3.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).''


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

"""

Sep 9 '05 #8

P: n/a
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'

Sep 9 '05 #9

P: n/a
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 && ma*@alcyone.com && 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
Sep 9 '05 #10

P: n/a
On 8 Sep 2005 22:48:05 -0700
"Johnny Lee" <jo************@hotmail.com> 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
Sep 10 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.