0

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 ?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
mittal
  • 915
  • 10
  • 29
  • "Since dict is un-ordered", not since python 3.5/3.6. – Jean-François Fabre Jun 18 '19 at 20:37
  • 1
    I didn't know that :). But anyways, I mentioned that code is in python 2.7 – mittal Jun 18 '19 at 20:40
  • can't you use `collections.OrderedDict` then? and sort when you dump the elements? for instance sort the keys so entity comes before status in an OrderedDict, dump that, and use the same trick when reloading (load+sort and put in ordered dict) – Jean-François Fabre Jun 18 '19 at 20:41
  • @Jean-FrançoisFabre Yes, I can do that. I wanted to understand that if this `type` invocation has the potential of failing in this case ? How it would work internally ? – mittal Jun 18 '19 at 20:45
  • um, `entity` and `_status` are instance attributes, so they shouldn't be in the dict passed to `type`, because that should only have class attributes – juanpa.arrivillaga Jun 18 '19 at 20:49
  • The `dict` argument to the `type` constructor can be any arbitrary mapping that implements (or defines all the methods for) the `collections.abc.Mapping` abstract base class. – Edward Minnix Jun 18 '19 at 21:01
  • @EdwardMinnix I didn't understand that comment – mittal Jun 19 '19 at 04:08
  • @juanpa.arrivillaga I was referring to an answer here https://stackoverflow.com/questions/1639174/creating-class-instance-properties-from-a-dictionary which said that `type()` could be used – mittal Jun 19 '19 at 04:10
  • That answer explicitly states that the dict is not for instance variables – juanpa.arrivillaga Jun 19 '19 at 05:10
  • @mittal I was referring to your question to @Jean-FrançoisFabre. The third argument to `type` can be any dict-like object (anything with string keys, __getitem__, and a way to iterate through those keys). Abstract base classes are just Python's alternative to interfaces in languages like Java, C#, or Go. – Edward Minnix Jun 19 '19 at 11:20
  • Thanks for all the suggestions here. I believe reading through the link I posted again, carefully, I realized that `type()` is not the correct way to create object instances as was correctly pointed by @juanpa.arrivillaga. I'll work with the other `setattr` method – mittal Jun 19 '19 at 18:36

0 Answers0