[Catalog-sig] Assistance with Python Classes

Skip Montanaro skip@pobox.com (Skip Montanaro)
Wed, 18 Jul 2001 11:28:37 -0500

    Graeme> I am a perl programmer learning python. I keep on getting an
    Graeme> exception error:

    Graeme> NameError: global name 'cgi' is not defined

    Graeme> I have a class called WebManager below

    Graeme> class WebManager:
    Graeme>      import os
    Graeme>      import cgi


It's because you import os and cgi inside the class scope, so you should be
referring to them as self.os and self.cgi or WebManager.os and
WebManager.cgi.  (Either will work if all you are doing is referring to
them.  If you want to change them and not create instance-specific copies of
them, you need to explicitly access them through the class.) 

How this wound up on catalog-sig is a question for another time.  I suspect
any of the following lists would be more appropriate for future questions
about Python:

    python-list@python.org      aka comp.lang.python

(though you should pick one and not post to all three).  As noted, you can
read python-list via the comp.lang.python newsgroup.  You don't subscribe to
python-help, just post to it.  The helpers there will make sure you're cc'd
on any responses.  The tutor list is open to all I believe.  Subscription
forms for the various Python-related mailing lists are available at


Skip Montanaro (skip@pobox.com)