[Tutor] Scope Issues...

Israel Evans israel@lith.com
Sun, 15 Jul 2001 08:23:22 -0700


The following kind of sums up what I was doing.  It's a simplified version
the merely demonstrates the issues I was having trouble with... Namely scope
stuff...
  these three functions need to work together in this way:
caller calls lister and will do reverser on the values returned by lister.

def lister():
	mylist = []		#initializes an empty list
	listcont = ['a','b','c']#your basic list
	for item in listcont:	#adds each item in listcont to mylist
		mylist.append(item)
	return mylist		#returns mylist

def reverser(list):		#takes a list 
	list.reverse()		#and reverses it

	return list		#then returns it

def caller():			#makes a local variable equal
	newlist = reverser(lister())#to the results returned by 
	print newlist		#both previous functions.


The problem was that in caller() I was just running the lister and
reverser by themselves without having any local variables there to
catch their return values. like so...
		
def badcaller():
	lister()         #run function without home for return value
	for item in mylist:	#try to access out of scope local variable
		reverser(item)	#even though mylist was "returned"
				# it needed to be caught by something,
				# otherwise it just disappears.

Silly oversight that indicates, I need to pay more attention to basics
before leaping ahead to more advanced stuff!  Though I do have to say
getting in over your head is the best way to learn!  You have so many
mistakes from which to be educated!

Thanks folks!
Israel...

-----Original Message-----
From: D-Man
To: [tutor]
Sent: 7/13/2001 4:16 PM
Subject: Re: [Tutor] Scope Issues...

On Fri, Jul 13, 2001 at 03:21:39PM -0700, Israel Evans wrote:
| 
| Whoo!   I sure seem to be on this list a lot lately!  Thanks for
bearing
| with me though.
| 
| 
| Ok.. My latest challenge is causing me headaches....
| 
| I've got three little function.
| 
| The first walks through a directory and builds a list of files that
have a
| specific extension.
| The second goes through a file and swaps one word with another.
| The third orchestrates the thing.  It calls the first function and
gets the
| list and performs the second function on every item in the list.
| 
| Where I'm having trouble is in getting the list built by the first
function.
| 
| At first, I defined the empty list in the top function and had that
function
| return the list after I've added stuff to it.
| It seems the list goes out of scope when I do it this way.

Unless you do something really weird, the reference to the list
doesn't go out of scope until the function returns.

| Then I tried making the list a global variable and by having the top
| function returning, I would thereby change the list.  It seems that
when I
| do it this way I get "local variable 'result' referenced before
assignment".
| And it seems that this error is called both for the top function and
the one
| that recurses over the values of the list.

If you have something like this :

bar = []
def foo() :
    print bar
    bar = bar + [ baz ]

you will get that error message.  Basically the assignment in the
function means that bar is a local variable.  Then you try and print
bar (the local variable) before you have created it.  In old (pre
2.1's nested scopes) python it wouldn't have been an error but it
would have been a subtle bug : sometimes you would refer to the global
'bar' and sometimes the 'local' bar which overshadows the global bar.
Bad.

| I've been reading the documentation, but it seems I'm not
understanding some
| of the basic scoping issues.  Any suggestions?

Show us the code, then we can show you the problem you really have.
Otherwise we have to guess at what is confusing you :-).

-D


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor