Python's setdefault allows you to get a value from a dictionary, but if the key doesn't exist, then you assign the based on parameter default
. You then fetch whatever is at the key in the dictionary.
Without manipulating an object's __dict__
Is there a similar function for objects?
e.g.
I have an object foo
which may or may not have attribute bar
. How can I do something like:
result = setdefaultattr(foo,'bar','bah')