[Python-Dev] properties on modules? (original) (raw)

Neil Schemenauer nas@python.ca
Mon, 13 Jan 2003 12:30:47 -0800


Skip Montanaro wrote:

I think the question is still open whether or not modules should be able to support properties, though I do think the ball is back in his court to come up with a less contrived example.

The Quixote web system publishes Python functions by traversing name spaces. The publishing code is very roughly:

def get_object(container, name):
    if hasattr(container, name):
        return getattr(container, name)
    elif isinstance(container, ModuleType):
        mname = container.__name__ + '.' + name
        __import__(mname)
        return sys.modules[mname]
    else:
        raise TraversalError

def publish(path):
    o = root_namespace
    for component in '/'.split(path[1:]):
        o = get_object(o, component)
    return o()

If you use instances for name spaces then you can use getattr or properties to lazily create attributes. It's annoying that there is nothing like getattr/setattr or properties for modules.

Neil