Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created May 23, 2023 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbelderbos/d76c9143675bf38ba89ce2a62dd2e5a1 to your computer and use it in GitHub Desktop.
Save bbelderbos/d76c9143675bf38ba89ce2a62dd2e5a1 to your computer and use it in GitHub Desktop.
>>> class Dog:
... def __init__(self, breed, color):
... self.breed = breed
... self.color = color
...
>>> dog1 = Dog("bulldog", "brown")
>>>
>>> dog1
<__main__.Dog object at 0x7fe114d36580>
>>> dog1.attr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Dog' object has no attribute 'attr'
>>> getattr(dog1, "breed")
'bulldog'
>>> getattr(dog1, "attr")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Dog' object has no attribute 'attr'
>>> getattr(dog1, "attr", "default_value")
'default_value'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment