Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > variable scope question?

Reply
Thread Tools

variable scope question?

 
 
globalrev
Guest
Posts: n/a
 
      05-13-2008
http://mail.python.org/pipermail/pyt...er/233435.html

why isnt it printing a in the second(second here, last one in OP)
example before complaining?

def run():
a = 1
def run2(b):
a = b
print a
run2(2)
print a
run()

def run():
a = 1
def run2(b):
print a
a = b
run2(2)
print a
run()
 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      05-13-2008
globalrev wrote:
> http://mail.python.org/pipermail/pyt...er/233435.html
>
> why isnt it printing a in the second(second here, last one in OP)
> example before complaining?
>
> def run():
> a = 1
> def run2(b):
> a = b
> print a
> run2(2)
> print a
> run()
>
> def run():
> a = 1
> def run2(b):
> print a
> a = b
> run2(2)
> print a
> run()
> --
> http://mail.python.org/mailman/listinfo/python-list
>

If you had told us what error you got, I would have answered you hours
ago. But without that information I ignored you until is was
convenient to run it myself. Now that I see no one has answered, and I
have time to run your examples, I see the error produced is

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in run
File "<stdin>", line 4, in run2
UnboundLocalError: local variable 'a' referenced before assignment

and its obvious what the problem is.

In run2 (of the second example), The assignment to a in the line "a=b"
implies that a *must* be a local variable. Python's scoping rules say
that if "a" is a local variable anywhere in a function, it is a local
variable for *all* uses in that function. Then it's clear that "print
a" is trying to access the local variable before the assignment gives it
a value.

You were expecting that the "print a" pickups it the outer scope "a" and
the assignment later creates a local scope "a", but Python explicitly
refuses to do that.

Gary Herron

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57