¿Por qué este decorador no puede importarse desde un módulo con "from module import *"?

I have the following decorator inside a module:

class CachedProperty(object):
    """Decorator that lazy-loads the value of a property.

    The first time the property is accessed, the original property function is
    executed. The value it returns is set as the new value of that instance's
    property, replacing the original method.
    """

    def __init__(self, wrapped):
        self.wrapped = wrapped
        try:
            self.__doc__ = wrapped.__doc__
        except:
            pass

    def __get__(self, instance, instance_type=None):
        if instance is None:
            return self

        value = self.wrapped(instance)
        setattr(instance, self.wrapped.__name__, value)

        return value

I want to import this decorator and other stuff from the module like this:

from clang.cindex import *

But I'm unable to import that single decorator this way, it works if I do:

from clang.cindex import CachedProperty

entonces puedo usar @CachedProperty.

Why can't I import this class through * while I'm able to import others?

preguntado el 25 de agosto de 12 a las 05:08

import * tends to be frown upon since it can create name collisions, Im curious what error do you get? -

While decorating some property with @CachedProperty I get an error saying CachedProperty is not defined. -

I know it's frowned upon, but I still want to know why it doesn't work. There's no name collisions happening. -

Not trying to insult your intelligence, but are you sure that CachedProperty is defined inside the module you're importing? Is clange.cindex a subpackage? If so, you'll have to set the __all__ variable in the module to tell python what names to import when you do from _ import * -

1 Respuestas

See if there is a variable named __all__ defined near the top of your module. If so it will have a sequence (list or tuple) of string names assigned to it. These are the public names of the module which are imported by the from ... import * .

Sin el __all__ name defined, all names defined in a module (as well as names imported from other modules), which do not begin with an underscore, are considered to be public.

Asegúrese de que el __all__ sequence includes the string "CachedProperty".

Respondido 25 ago 12, 07:08

Thanks, I'm new to Python, __all__ was at the bottom of the 3000 lines module... - pepper_chico

Glad I could help. I've never seen __all__ defined near the bottom of a module before. Makes more sense to put it near the top where it helps to document the modules public names. In fact I tend to put one name per line with a short commentary description after each. - Don O'Donnell

It seems I just got bad luck getting acquainted with python's __all__ =). Just take a look, down the bottom, without any docs: llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/… - pepper_chico

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.