I am writing a small class that has one method: get_state
. It is parameterless and returns a dictionary of all the names in self (look in its dict) that are not bound to function objects (test functions by using inspect.isfunction on the objects). When a class inherits from Dumpable
, its instances can call get_state()
to get a dictionary of all the names in its namespace (that are not functions) and their values.
For example, if we first defined class Modular_Counter(Dumpable)
and then mc = Modular_Counter(0,3)
and then print(mc.get_state())
, it would print the following dictionary: {'_value': 0, '_modulus': 3}
.
Here is my current working code for the class:
class Dumpable:
def get_state(self):
no_funs = self.__dict__
no_fun_list = [no_fun_key for no_fun_key in no_funs.keys() if inspect.isfunction(no_funs[no_fun_key])]
for fun in no_fun_list:
del no_funs[fun]
return no_funs
Is there a cleaner way to get the same results? I still need to use the one function in the class. Any help would be appreciated.