oblitum
oblitum

Reputation: 12016

Why this decorator can't get imported from module with "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

then I can use @CachedProperty.

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

Upvotes: 3

Views: 1601

Answers (1)

Don O'Donnell
Don O'Donnell

Reputation: 4728

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 * statement.

Without the __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.

Make sure the __all__ sequence includes the string "CachedProperty".

Upvotes: 4

Related Questions