本文整理汇总了Python中builtins.getattr方法的典型用法代码示例。如果您正苦于以下问题:Python builtins.getattr方法的具体用法?Python builtins.getattr怎么用?Python builtins.getattr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类builtins
的用法示例。
在下文中一共展示了builtins.getattr方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __enter__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import getattr [as 别名]
def __enter__(self):
assert getattr(self._globals, 'default_graph', None) is None, \
"cannot have more than one default graph"
Graph._globals.default_graph = self
return self
示例2: evaluate_operation
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import getattr [as 别名]
def evaluate_operation(cls, operation, context, **kwargs):
"""
Evaluate an operation or constant given a context.
"""
try:
if isinstance(operation, Operation):
return operation.evaluate(context, **kwargs)
partial = functools.partial(cls.evaluate_operation, context=context, **kwargs)
if isinstance(operation, tuple):
return tuple(partial(element) for element in operation)
if isinstance(operation, list):
return [partial(element) for element in operation]
if isinstance(operation, dict):
return {partial(key): partial(value) for key, value in operation.items()}
if isinstance(operation, slice):
return slice(*[partial(getattr(operation, attr))
for attr in ['start', 'stop', 'step']])
return operation
except Exception as ex: # pragma: no cover
stack = []
interactive = False
for frame in reversed(operation._stack): # pylint: disable=protected-access
# Do not capture any internal stack traces
if 'pythonflow' in frame.filename:
continue
# Stop tracing at the last interactive cell
if interactive and not frame.filename.startswith('<'):
break # pragma: no cover
interactive = frame.filename.startswith('<')
stack.append(frame)
stack = "".join(traceback.format_list(reversed(stack)))
message = "Failed to evaluate operation `%s` defined at:\n\n%s" % (operation, stack)
raise ex from EvaluationError(message)
示例3: getattr
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import getattr [as 别名]
def getattr(obj, attr, *args):
'''Replacement for the built-in
:func:`getattr() <python:getattr>` function.'''
return builtins.getattr(obj, attr, *args)
示例4: getattr_or
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import getattr [as 别名]
def getattr_or(default, attr, obj):
"""
Return given attribute of object or the default argument if attr is not
given.
"""
return _builtins.getattr(obj, attr, default)