473,594 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ 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.__somethin g variable
in the foo.__init__?

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

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

returns the following traceback:

Traceback (most recent call last):
File
"C:\Python22\Li b\site-packages\Python win\pywin\frame work\scriptutil s.py",
line 301, in RunScript
exec codeObject in __main__.__dict __
File "F:\SYSCON\SMAR TROUTE\classjun k.py", line 13, in ?
x=bar()
File "F:\SYSCON\SMAR TROUTE\classjun k.py", line 10, in __init__
foo.__init__()
File "F:\SYSCON\SMAR TROUTE\classjun k.py", line 3, in __init__
print self.__somethin g
AttributeError: bar instance has no attribute '_foo__somethin g'

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 1224
In article <P6************ ********@comcas t.com>,
"Larry Bates" <lb****@swamiso ft.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.__somethin g 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.__somethin g
class bar(foo):
__something="Th is is a test"

def __init__(self):
foo.__init__(se lf)
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__(se lf)
x = bar()

it works fine.
Jul 18 '05 #2
On Thu, 25 Mar 2004 16:03:49 -0600, "Larry Bates"
<lb****@swamiso ft.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.__somethin g variable
in the foo.__init__?

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

def __init__(self):
foo.__init__(se lf)
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\L ib\site-packages\Python win\pywin\frame work\scriptutil s.py",
line 301, in RunScript
exec codeObject in __main__.__dict __
File "F:\SYSCON\SMAR TROUTE\classjun k.py", line 13, in ?
x=bar()
File "F:\SYSCON\SMAR TROUTE\classjun k.py", line 10, in __init__
foo.__init__()
File "F:\SYSCON\SMAR TROUTE\classjun k.py", line 3, in __init__
print self.__somethin g
AttributeError : bar instance has no attribute '_foo__somethin g'
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
3183
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
2866
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 maintain encapsulation by leaving it inside the loop? Common stuff for examples: class Data;
0
1441
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 learning c# the hard way, but the similarities to Java are very helpful. However, in this case they are hurting, because I can't figure out the best way to store a class that can be accessed across the scope of an ASP.NET application. To begin with,...
7
2659
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 before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the beginning of a method or whether they will allocate the memory as it is needed by the method to...
166
8574
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
2267
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. For example, say I have the following script <?php constant myvar = "my original string";
1
1458
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 TheOneItsIn.Variable But is there a way to give a variable Module scope so it can be used in all the classes without a prefix? Thanks
5
16381
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 = 0; NextVal = (++NextVal) % 317; return (NextVal); }
1
25648
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 bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
112
5394
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 that may print some messages. foo(...) { if (!silent)
0
7954
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
8259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8016
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
8244
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6669
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
5836
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
3871
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3905
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
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 we have to send another system

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.