473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the difference between VAR and _VAR_?

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
10 2839
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
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
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

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

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

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__spa m, 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''inst ance 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__directl y. """

"""
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 internationaliz ation; 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
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.initialNum ber = userValue
self.__secretSe ed = 7
return self.initialNum ber * self.__secretSe ed
enc = encrypt()
enc.encrypt(5) 35 print enc.initialNumb er 5 print enc.__secretSee d


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

Sep 9 '05 #9
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14703
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
5
20598
by: radnus2004 | last post by:
Hi all, I am not clear what is the difference between signed and unsigned in C. Many say, unsigned means only +ve values and 0. Can I use unsigned int i = -3? What happens internally ? What conversion happens? Also on 32 and 64bit machines what happens? Can anybody explain?
10
15646
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
13
5035
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
12
17388
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference, and I would like to know what it is so that I can take advantage of it in my code. Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
3
3015
by: Anoj Kumar | last post by:
Hi All , can anyone tell me what is the difference between the following declaration and how it affects application performance. 1. Dim cn As ADODB.Connection Set cn = New ADODB.Connection
6
11245
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What is the difference between a Field and a Property? Here is the context. In the VS.NET 2002 documentation, in the "String Members" section, we have the following...
5
5840
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. Thanks kai
49
2415
by: Zach | last post by:
After having taken a looong break from VB (last used 6.0), I started getting back into programming again, this time with VS 2005. I began reading up on VB.NET from various sources (books, internet, etc.) and found out about the CLR and how all of the .NET languages access it, the major difference being the syntax and structure of the individual languages. What I'm wondering, since VB.NET is obviously easier to learn/use than C#.NET and...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.