[Tutor] "local variable 'l1' referenced before assignment"

wesley chun wescpy at gmail.com
Sat Jun 28 21:57:08 CEST 2008


>  BTW I see that your book has what seems to be a thorough discussion of
> namespaces and variable scopes in chapter 12.

ah, that section (12.3) you're referring to is mostly about namespaces
as it's in the Modules chapter.  the real bit about variable scope
that you're seeking is actually in the immediately preceding chapter
on Functions in section 11.8, which does address some of the topics
covered in this thread. the reason why scope is mentioned in that ch12
section is because i introduce namespaces there, and it's easy for
those new to the language to get confused about the relationship b/w
namespaces and variable scope.

for example, and this is not recommended, of course, create a global
variable named 'str', then create another one in a function, like
this:

str = 'foo'
def func():
    str = 'bar'

so now the name 'str' lives in *3* different namespaces, local (to
func), global, and built-in. *but* from any point in your code, you
only have (legal) access to one of them -- that's scope. (side note: i
think 'str' and 'list' are the 2 most popular variables that
shadow/override built-in names.)

one thing that i did learn from all you tutors is the behavior for
augmented asst differing from that of the full asst syntax... i'll jot
down to make sure it's covered in the next edition.  :-)

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list