4 examples of 'python getattr' in Python

Every line of 'python getattr' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
51def builtins_getattr(evaluator, obj, params):
52 stmts = []
53 # follow the first param
54 objects = _follow_param(evaluator, params, 0)
55 names = _follow_param(evaluator, params, 1)
56 for obj in objects:
57 if not isinstance(obj, (er.Instance, er.Class, pr.Module, compiled.CompiledObject)):
58 debug.warning('getattr called without instance')
59 continue
60
61 for name in names:
62 s = unicode, str
63 if isinstance(name, compiled.CompiledObject) and isinstance(name.obj, s):
64 stmts += evaluator.follow_path(iter([name.obj]), [obj], obj)
65 else:
66 debug.warning('getattr called without str')
67 continue
68 return stmts
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

69def _try_getattr(object, name):
70 try:
71 attr = getattr(object, name)
72 except :
73 attr = _sentinel
74 return attr
318def __getattr__(self, name):
319 if name not in ['start_pos', 'end_pos', 'parent', 'asserts', 'raw_doc',
320 'doc', 'get_imports', 'get_parent_until', 'get_code',
321 'subscopes']:
322 raise AttributeError("Don't touch this: %s of %s !" % (name, self))
323 return getattr(self.base, name)
12def rgetattr(obj, attr, default=sentinel):
13 # https://stackoverflow.com/a/31174427/1472229
14 if default is sentinel:
15 _getattr = getattr
16 else:
17 def _getattr(inner_obj, name):
18 return getattr(inner_obj, name, default)
19 return functools.reduce(_getattr, [obj]+attr.split('.'))

Related snippets