beginner question (True False help)

MRAB python at mrabarnett.plus.com
Fri Aug 9 20:39:47 EDT 2013


On 10/08/2013 00:40, eschneider92 at comcast.net wrote:
> (I forgot to post this with my last post.)
> Also, I don't understand any part of the following example, so there's no specific line that's confusing me. Thanks for the help btw.
>
You don't understand _any_ of it?


 > var = 42

Here you're assigning to 'var'. You're not in a function, so 'var' is a
global variable.

> def  myfunc():
 >       var = 90

Here you're assigning to 'var'. If you assign to a variable anywhere in
a function, and you don't say that that variable is global, then it's
treated as being local to that function, and completely unrelated to
any other variable outside that function.
>
> print "before:", var
> myfunc()
> print "after:", var
>
> def myfunc():
>      global var
>      var = 90
>
Here you're assigning to 'var', but this time you've declared that it's
global, so you're assigning to the global variable called 'var'.




More information about the Python-list mailing list