我有一个模块内的以下装饰:为什么这个装饰器无法从“从模块导入*”模块导入?
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
我想导入这个装饰和其他的东西,从这样的模块:
from clang.cindex import *
但我无法导入单一装饰这样,它的工作,如果我这样做:
from clang.cindex import CachedProperty
然后我可以使用@CachedProperty
。
为什么我不能通过*
导入此类,而我能够导入其他类?
'import *'往往会因为可以创建名称冲突而皱眉,我很好奇你会得到什么错误? –
用@CachedProperty装饰一些属性时,我得到一个错误,说CachedProperty没有定义。 –
我知道这是不好的,但我仍然想知道为什么它不起作用。没有名称冲突发生。 –