Assigning to self.__class__

bruno at modulix onurb at xiludom.gro
Thu Jan 26 09:36:12 CET 2006

Paul McGuire wrote:
> 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.

Hooray ! You've just (re)discovered the state pattern... for which the
most stupid simple implementation in Python is to :

>(snip) assigning to self.__class__, (snip)


> Any comments on this practice?  

It can be very confusing for newbies and peoples having no experience
with *dynamic* languages, and I guess control-freaks and
static-typing-addicts would runaway screaming.  But I like it anyway !-)

> Is this intended capability for Python
> objects, 

AFAIK, yes.

> or am I taking advantage of a fortuitous accident, which may get
> undone at a future time?

It's certainly not  a fortuitous accident.

bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"

More information about the Python-list mailing list