Fixed keys() mapping
gagsl-py at yahoo.com.ar
Fri Jan 12 01:34:52 CET 2007
At Thursday 11/1/2007 14:19, George Sakkis wrote:
>The implementation I came up with goes like this: each fkdict instance
>stores only the values as a list in self._values. The keys and the
>mapping of keys to indices are stored in a dynamically generated
>subclass of fkdict, so that self._keys and self._key2index are also
>accessible from the instance. The dynamically generated subclasses are
>cached so that the second time an fkdict with the same keys is created,
>the cached class is called.
>Since the keys are determined in fkdict.__init__(), this scheme
>requires changing self.__class__ to the dynamically generated subclass.
>As much as I appreciate Python's dynamic nature, I am not particularly
>comfortable with objects that change their class and the implications
>this may have in the future (e.g. how well does this play with
>inheritance). Is this a valid use case for type-changing behavior or is
>there a better, more "mainstream" OO design pattern for this ? I can
>post the relevant code if necessary.
I think a better place would be __new__ instead. This is where you
can determine the right class to use and construct the new instance.
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
More information about the Python-list