Will getattr() create a missing attribute with the default value provided?


#1

Question

If a call is made to getattr() for an attribute which is not present and a default value is provided, will the function create the missing attribute and assign it the default value?

Answer

NO, the getattr() function will not create an attribute which is not present. If a default value is provided, it will return that value at the time getattr() is called, otherwise an AttributeError error will be raised if no default value is provided.