Addind imports to a class namespace

Ryan K ryankaskel at gmail.com
Fri Jul 10 21:57:43 EDT 2009


Greetings,

In order to avoid circular imports, I have a class that I want to
improve upon:

Class GenerateMenuXhtml(threading.Thread):
    """
    Subclasses a threading.Thread class to generate a menu's XHTML in
a separate
    thread. All Link objects that have this menu associated with it
are gathered
    and combined in an XHTML unordered list.

    If the sender is of type Link, then all menus associated with that
link are
    iterated through and rebuilt.
    """
    def __init__(self, instance):
        from asqcom.apps.staticpages.models import Menu, Link
        self.Link = Link
        self.Menu = Menu

As you can see I just expose these imports by attaching them to
members of the class. There must be "prettier" option though where I
can just add these imoprts to the class's namespace so all methods of
any instance will have access to the imported modules.

How would I go about doing this? How can I access the namespace of any
class? Through Class.__dict__?

Thanks,
Ryan




More information about the Python-list mailing list