473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

scoping questions

Hi,

Another of my crazy questions. I'm just in the process of learning so bear
with me if you can. I actually ran it.... with two test cases

TEST CASE 1:
Say I have the following defined:
--- beginning of code snippet ----

def me(aFile):
"""
Note I am testing scoping
"""
aFile = 'hello world'
print aFile

aFile = open('/tmp/test','r')
me(aFile)
data = aFile.read()
print data

------ end of code snippet ----
my test file has a sentence 'This is a test of the /tmp/test file'

When I run it I observed this output:
hello world
This is a test of the /tmp/test file

Now what this means to me and this is where I need your help:

When I call the 'me' function, its passing a reference to my original aFile
variable.
Inside the me function, I'm guessing it is now a new reference to the same
original aFile contents. When I assign it to a simple string, it simply
changes the local reference to point to that string. Since its a copy of
the reference, it doesn't affect the caller's value.

In essence if i understand correctly

At the global scope I have

variable aFile that points to an instance of a 'file'

Inside the me function scope I have
a parameter named aFile that is a local copy of the original reference of
what global::aFile was pointing to.
Essentially local::aFile is pointing to a file object

At this point I have two references to the file object.

When I assign the new value to aFile (local) it simply does the assignment.
The global reference is untouched.

I would sort of expect this behavior as I am not returning anything

If test #1 is true, then test case 2 boggles me a bit

TEST CASE 2:
-------
def me(aFile):
"""
Note I am testing scoping
"""
aFile = 'hello world'
print aFile
def me2(dog):
"""
Note I am testing scoping
"""
print "Inside me2"
dog.close()
aFile = open('/tmp/test','r')
me(aFile)
data = aFile.read()
print "test1", data
aFile.close()
aFile = open('/tmp/test','r')

me2(aFile)
print "test2", aFile.read()
=====

It bombs out on the last line because aFile was closed in the function
'me2'.

Perhaps the way to explain it is that Inside me2 when my copy of the
original reference is made, I have a global and local variable both
referencing the same 'object'. I am able to do operations in the local me2
scope and have them effect the behavior of the global scope.

Its just a bit confusing because python is apparently smart enough to
realize that my action on the local reference hasn't redefined the
capability of the global so it has allowed this to occur on the actual
global file referenced object. (as opposed to just a local copy). And I
didn't return anything....
I'm just confused a bit here.... Perhaps someone can clarify

Thanks

David
-------
Tracfone: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html

__________________________________________________ _______________
Looking to buy a house? Get informed with the Home Buying Guide from MSN
House & Home. http://coldwellbanker.msn.com/
Jul 18 '05 #1
2 1624
David Stockwell wrote:
Hi,

Another of my crazy questions. I'm just in the process of learning so
bear with me if you can. I actually ran it.... with two test cases

TEST CASE 1:
Say I have the following defined:
--- beginning of code snippet ----

def me(aFile):
"""
Note I am testing scoping
"""
aFile = 'hello world'
print aFile

aFile = open('/tmp/test','r')
me(aFile)
data = aFile.read()
print data

------ end of code snippet ----
my test file has a sentence 'This is a test of the /tmp/test file'

When I run it I observed this output:
hello world
This is a test of the /tmp/test file

Now what this means to me and this is where I need your help:

When I call the 'me' function, its passing a reference to my original
aFile variable.
Inside the me function, I'm guessing it is now a new reference to the
same original aFile contents. When I assign it to a simple string, it
simply changes the local reference to point to that string. Since its a
copy of the reference, it doesn't affect the caller's value.

In essence if i understand correctly

At the global scope I have

variable aFile that points to an instance of a 'file'

Inside the me function scope I have
a parameter named aFile that is a local copy of the original reference
of what global::aFile was pointing to.
Essentially local::aFile is pointing to a file object

At this point I have two references to the file object.

When I assign the new value to aFile (local) it simply does the
assignment. The global reference is untouched.

I would sort of expect this behavior as I am not returning anything

If test #1 is true, then test case 2 boggles me a bit

TEST CASE 2:
-------
def me(aFile):
"""
Note I am testing scoping
"""
aFile = 'hello world'
print aFile
def me2(dog):
"""
Note I am testing scoping
"""
print "Inside me2"
dog.close()
aFile = open('/tmp/test','r')
me(aFile)
data = aFile.read()
print "test1", data
aFile.close()
aFile = open('/tmp/test','r')

me2(aFile)
print "test2", aFile.read()
=====

It bombs out on the last line because aFile was closed in the function
'me2'.

Perhaps the way to explain it is that Inside me2 when my copy of the
original reference is made, I have a global and local variable both
referencing the same 'object'. I am able to do operations in the local
me2 scope and have them effect the behavior of the global scope.

Its just a bit confusing because python is apparently smart enough to
realize that my action on the local reference hasn't redefined the
capability of the global so it has allowed this to occur on the actual
global file referenced object. (as opposed to just a local copy). And I
didn't return anything....
I'm just confused a bit here.... Perhaps someone can clarify

Thanks

David
-------
Tracfone: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html

__________________________________________________ _______________
Looking to buy a house? Get informed with the Home Buying Guide from MSN
House & Home. http://coldwellbanker.msn.com/

Hello,
I'm not sure I've understood entirely, but I'll try to explain: When you
have a reference to an object, you can do anything you like with it -
close it if it's a file, for example. When you write, if the function
'me', aFile = 'hello world', you simply discard your reference to the
file, and create a new reference, called 'aFile' too, to the string
'hello world' you've created. You do all this in the local namespace, so
it doesn't affect the global namespace.
Maybe this is the point that should be clarified: There's only one pool
of objects. What's global and local are the namespaces - references to
those objects.

I hope this helped,
Noam Raphael
Jul 18 '05 #2
In article <ma*************************************@python.or g>,
"David Stockwell" <wi*******@hotmail.com> wrote:
Another of my crazy questions. I'm just in the process of learning so bear
with me if you can. I actually ran it.... with two test cases

TEST CASE 1:
Say I have the following defined:
--- beginning of code snippet ----

def me(aFile):
"""
Note I am testing scoping
"""
aFile = 'hello world'
print aFile

aFile = open('/tmp/test','r')
me(aFile)
data = aFile.read()
print data


Yet another way to think of it, since name spaces are
dictionary-like and we can "follow" the Python engine as it
runs through the subroutines:
things = {}
things['aFile'] = open ('/tmp/test/, 'r')

me_things = {}
me_things['aFile'] = things['aFile']
me_things['aFile'] = 'hello world'
print me_things['aFile']

things['data'] = things['aFile'].read()
print things['data']
See how that works? So in the next example, when you
have
me2_things['dog'] = things['aFile']
me2_things['dog'].close()
it's not really a surprise.

Regards. Mel.
Jul 18 '05 #3

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

Similar topics

8
by: Ian McMeans | last post by:
I was bitten by a bug today that depended on how lambda works. It took me quite a while to realize what was going on. First, I made multiple lambda functions inside a loop, each of which...
2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
4
by: Frederick Grim | last post by:
okay so the code in question looks like: namespace util { template<typename C> inline void mmapw(C *& ptr, size_t length, int prot, int flags, int fd, off_t offset) { if((ptr =...
1
by: Michael | last post by:
I am having a problem with scoping of parameters in my XSLT Stylesheet...here is the stylesheet (the xml document is irrelevant for the example) <?xml version="1.0" encoding="UTF-8"?>...
4
by: Joel Gordon | last post by:
Hi, When I try and compile the a class containing the following method : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i /...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
3
by: SPECTACULAR | last post by:
Hi all. I have a question here.. what kind of scoping does C++ use? and what kind does Smalltalk use? I know smalltalk is a little bit old .. but any help would be appreciated.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.