Code: Select all
def getattr(obj, name, default=None):
try:
# Try to get the attribute
return object.__getattribute__(obj, name)
except AttributeError:
# If the attribute does not exist, check whether a default value has been passed
if default is not None:
return default
# If no default value is specified, trigger the AttributeError
raise
Code: Select all
class ExampleClass:
def __init__(self):
self.example_value = "example"
test = ExampleClass()
test_getattr_not_exists = getattr(test, "unknown_value", None)
print(test_getattr_not_exists) # Output : None
Code: Select all
test_getattr_not_exists
als Nachricht:
Code: Select all
AttributeError: 'ExampleClass' object has no attribute 'unknown_value'