Python setattr、getattr、hasattr
setattr(obj, attr, value)
is a built-in Python function that sets the value of a given attribute (attr) of an object (obj) to the specified value.
getattr(obj, attr, default)
is a built-in Python function that returns the value of a given attribute (attr) of an object (obj). If the attribute doesn't exist, it returns the default value specified. If the default value is not specified and the attribute doesn't exist, it raises an AttributeError.
hasattr(obj, attr)
is a built-in Python function that checks if an object (obj) has a given attribute (attr). It returns True
if the attribute exists, and False
otherwise.