[Courses] [python] Lesson 3: variable scope

Peggy Russell prusselltechgroup at gmail.com
Thu Jul 7 22:25:35 UTC 2011


> Brad and Monique wanted to know about variable scoping. This is
> a relatively advanced topic that will probably only make sense to
> folks who have done a fair amount of programming in other languages.
> So, beginners, feel free to skip this message -- you won't need it
> for anything in this class.
> ...

I'm glad someone asked :-) 
I was searching for the answer too.

I saw this FAQ at python.org: 

http://www.python.org/doc//current/faq/programming.html#id10

What are the rules for local and global variables in Python?
------------------------------------------------------------
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as ‘global’.
...

I tried python's help to find the answer. I typed `help()`, `topics`, 
and last, `SCOPING` (which appeared in the list).  

Just a side note: 
 - ctrl-d exits python's help (like interactive python)
 - the help seems to use a pager similar to less. I don't know if that is
   configurable. I'm on linux.
   -- q exits less   
   -- moving up/down: arrows, Page Up/Page Down, space bar
   -- top: g; bottom: G

Thanks,
  Peggy


More information about the Courses mailing list