def get_value_from_name(name, globs=None):
"""
Given a name, return the corresponding value.
@param globs: A namespace to check for the value, if there is no
module containing the named value. Defaults to __builtin__.
"""
name = DottedName(name)
# Import the topmost module/package. If we fail, then check if
# the requested name refers to a builtin.
try:
module = _import(name[0])
except ImportError, e:
if globs is None: globs = __builtin__.__dict__
if name[0] in globs:
try: return _lookup(globs[name[0]], name[1:])
except: raise e
else:
raise
# Find the requested value in the module/package or its submodules.
for i in range(1, len(name)):
try: return _lookup(module, name[i:])
except ImportError: pass
module = _import('.'.join(name[:i+1]))
module = _lookup(module, name[1:i+1])
return module
评论列表
文章目录