473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Variable scope in classes

I'm confused about variable scope in a class
that is derived from a base class framework.

Can someone enlighten me on why bar.__init__
method can't see self.__something variable
in the foo.__init__?

Example:
class foo:
def __init__(self):
print self.__something
class bar(foo):
__something="This is a test"

def __init__(self):
foo.__init__(self)
x=bar()

returns the following traceback:

Traceback (most recent call last):
File
"C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py" ,
line 301, in RunScript
exec codeObject in __main__.__dict__
File "F:\SYSCON\SMARTROUTE\classjunk.py", line 13, in ?
x=bar()
File "F:\SYSCON\SMARTROUTE\classjunk.py", line 10, in __init__
foo.__init__()
File "F:\SYSCON\SMARTROUTE\classjunk.py", line 3, in __init__
print self.__something
AttributeError: bar instance has no attribute '_foo__something'

Is there some "other" way to do this without passing information
through foo.__init__ argument list?

Regards,
Larry Bates
Syscon
Jul 18 '05 #1
2 1219
In article <P6********************@comcast.com>,
"Larry Bates" <lb****@swamisoft.com> wrote:
I'm confused about variable scope in a class
that is derived from a base class framework.

Can someone enlighten me on why bar.__init__
method can't see self.__something variable
in the foo.__init__?

Because __something is "private" to bar, and thus not visible anywhere
else (unless you manually tweak on the "_foo", which is tricky and
really not very nice).
Example:
class foo:
def __init__(self):
print self.__something
class bar(foo):
__something="This is a test"

def __init__(self):
foo.__init__(self)
x=bar()


If you get rid of the private underscore:

class foo:
def __init__(self):
print self.something
class bar(foo):
something="This is a test"

def __init__(self):
foo.__init__(self)
x = bar()

it works fine.
Jul 18 '05 #2
On Thu, 25 Mar 2004 16:03:49 -0600, "Larry Bates"
<lb****@swamisoft.com> wrote:
I'm confused about variable scope in a class
that is derived from a base class framework.

Can someone enlighten me on why bar.__init__
method can't see self.__something variable
in the foo.__init__?

Example:
class foo:
def __init__(self):
print self.__something
class bar(foo):
__something="This is a test"

def __init__(self):
foo.__init__(self)
x=bar()

returns the following traceback:
So why did you put __ in front of 'something' if you were not
intending this to be a "private" variable?
Traceback (most recent call last):
File
"C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py" ,
line 301, in RunScript
exec codeObject in __main__.__dict__
File "F:\SYSCON\SMARTROUTE\classjunk.py", line 13, in ?
x=bar()
File "F:\SYSCON\SMARTROUTE\classjunk.py", line 10, in __init__
foo.__init__()
File "F:\SYSCON\SMARTROUTE\classjunk.py", line 3, in __init__
print self.__something
AttributeError: bar instance has no attribute '_foo__something'
Python makes the the variable "private" by prepending _foo to whatever
you name with two leading underscores.

-- Dave
Is there some "other" way to do this without passing information
through foo.__init__ argument list?

Regards,
Larry Bates
Syscon


Jul 18 '05 #3

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

Similar topics

6
3174
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
3
2854
by: Thomas Matthews | last post by:
Hi, While coding programs, I cam about a conundrum regarding variables defined in an iterative loop. The issue is whether it is more efficient to factor the definition out of the loop or...
0
1430
by: Joe Blow via DotNetMonster.com | last post by:
Hello, I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm...
7
2634
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
166
8471
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
2262
by: Joe Molloy | last post by:
Hi, I'm wondering is there any way I can get a variable's value from within a class when the variable has been declared outside the class but in the same script as the class is contained in. ...
1
1441
by: Just Me | last post by:
Given a file containing Multiple classes and a variable that needs to be seen in all the classes I can put it in one of the classes as Shared and reference it the other classes as ...
5
16366
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
1
25612
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
112
5338
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
7033
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,...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6726
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...
0
5318
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,...
0
4468
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...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.