[Catalog-sig] Assistance with Python Classes
John J. Lee
Wed, 18 Jul 2001 18:54:08 +0100 (BST)
On Wed, 18 Jul 2001, Graeme Matthew wrote:
> Hi there can someone please assist me
You're posting to the wrong place. Try comp.lang.python, or (I think)
> I am a perl programmer learning python. I keep on getting an exception error:
> NameError: global name 'cgi' is not defined
You're importing the cgi module into class scope (not something I ever
do, personally, but YMMV I suppose), so you need to qualify it:
cgiObj = self.cgi.cgi()
which will work inside a method because self (the class instance) has been
passed in to the method as its first argument, as in Perl. The thing to
remember is that (prior to 2.2, or 2.1 with from __future__ import ...)
Python has three scopes: builtin, (module) global, and local; the local
scope is function-local inside functions (hence methods), class-local
inside class definitions, etc.
It's all in the tutorial!
(under 'Python scopes and namespaces')
You have picked a time to learn Python where all this is about to change,
though, so you may want to check out the from __future__ import foo thing,
where foo is 'nested_scopes' or something -- see the 2.1 docs.