Assigning to self.__class__
Terry Reedy
tjreedy at udel.edu
Thu Jan 26 15:38:47 EST 2006
"Paul McGuire" <ptmcg at austin.rr._bogus_.com> wrote in message
news:_G%Bf.1219$jO.730 at tornado.texas.rr.com...
>I have some places in pyparsing where I've found that the most
> straightforward way to adjust an instance's behavior is to change its
> class.
> I do this by assigning to self.__class__, and things all work fine.
>
> (Converting to use of __new__ is not an option - in one case, the change
> is
> temporary, and before the end of the function, I change it back again.)
>
> Any comments on this practice? Is this intended capability for Python
> objects, or am I taking advantage of a fortuitous accident, which may get
> undone at a future time?
Very little if any of the new class stuff is accidental. As I remember,
.__class__ was originally read-only and was intentionally changed when
practical use cases for rewriting it were developed and presented with a
request for change. Though I could be wrong, I would comfortably use this
feature myself.
Terry J. Reedy
More information about the Python-list
mailing list