Assigning to __class__ : bad form?

The Pythonista none at this.time
Fri Jun 6 19:51:14 CEST 2008

I've been wondering for a while about whether assigning to __class__ is 
bad form or not.  Specifically, I mean doing so when some other method of 
implementing the functionality you're after is available (i.e. using an 
adapter, or something like the strategy pattern).

To give an example and a non-example of what I'm talking about, consider 
the following recipes from the online Python Cookbook:

Ring Buffer:

In this case, I think the assignment to __class__ just obfuscates things, 
and the example would be better coded as a single class.

On the other hand,

Fast copy of an object having a slow __init__ : http://

This seems like a reasonable use case for assigning to __class__ (except 
that it's already implemented in the 'new' module, but, read the Martelli-
bot's comments near the end of the recipe).  I consider this a reasonable 
use case for assigning to __class__, because, short of the 'new' module, 
I don't see any other way to accomplish it.

So, what is the opinion of the broader Python community?  Is code that 
assigns to __class__ just clever trickiness to be avoided, or is it 
sometimes a good thing?

-- A blog about life, the universe, and Python
** Posted from **

More information about the Python-list mailing list