Hi,
I am trying to create the most general function possible to test if an object passed into it has mutability or not. With some prior help from pytony, it seemed that the best way to do this is to try to set and attribute. If the attribute can be set, it is mutable, otherwise it will force an error.
To make this function general, I need to be able to pick an attribute from the object. I thought the best way to do this would be to list the attributes, pick the first one listed and then overwrite it. It would be superfulous to try to overwrite every attribute, no?
So the steps are:
- Take in object.
- Make a deepcopy so as not to overwrite the object.
- List its attributes.
- Choose a single attribute.
- Try running setattr on this.
- Infer mutability based whether or not errors appear.
In regard to step 2, I"m confused on the most general way to get class attribtues in Python. From another website, the recommended way was:
>>> class new_class():
... def __init__(self, number):
... self.multi = int(number) * 2
... self.str = str(number)
...
>>> a = new_class(2)
>>> a.__dict__
{'multi': 4, 'str': '2'}
>>> a.__dict__.keys()
dict_keys(['multi', 'str'])
This seems messy and can it be guaranteed that all mutable python objects will have the dict variable? Does the presence of the dict variable automatically indicate mutability?