How to use 'python hasattr' in Python

Every line of 'python hasattr' 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
64def _hasattr(obj, attr_name):
65 # If possible, avoid retrieving the attribute as the object might run some
66 # lazy computation in it.
67 if attr_name in dir(obj):
68 return True
69 try:
70 getattr(obj, attr_name)
71 except AttributeError:
72 return False
73 else:
74 return True
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


Related snippets