本文整理匯總了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)