Background
I have a custom class where 1 particular attribute is dependent on another. Working with python 2.7
I know, not a good design, but that's what it is for now
class CrazyClass(object):
def __init__():
self.entity = None # This will be None initialized. Later populated as a dict
self._status = 'undefined'
@property
def status(self):
return self._status
@status.setter
def status(self, value):
self._status = value
try:
self.entity["Status"] = value
except Exception:
raise Exception("'entity' should be defined before specifying 'status'")
So basically, if you wish to set status
, entity
should have already been defined
Requirement
I need to dump the class object in a file and then later read it and recreate the object instance
I am not using pickle/unpickle and instead dumping the attributes and values as key/value pairs in a dictionary.
Instead I am trying to use type(name, bases, dict)
method
The Dilemma
While it seems to work fine but I am really skeptical if it will always work ??
type('CrazyClass',(object,),dumped_dict)
Since dict
is un-ordered, could there be case that status
is populated before entity
and this fails ?
Any insight on how type
would work internally ?