469,917 Members | 1,746 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

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 1160
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Brian Jones | last post: by
3 posts views Thread by Thomas Matthews | last post: by
reply views Thread by Joe Blow via DotNetMonster.com | last post: by
166 posts views Thread by Graham | last post: by
6 posts views Thread by Joe Molloy | last post: by
pbmods
1 post views Thread by pbmods | last post: by
112 posts views Thread by istillshine | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.