scoping questions
Mel Wilson
mwilson at the-wire.com
Thu Jun 3 09:05:05 EDT 2004
In article <mailman.519.1086198524.6949.python-list at python.org>,
"David Stockwell" <winexpert at 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.
More information about the Python-list
mailing list