scoping questions

David Stockwell winexpert at hotmail.com
Wed Jun 2 19:47:31 CEST 2004


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/





More information about the Python-list mailing list