Unification of Methods and Functions

Duncan Booth me at privacy.net
Tue Jun 1 17:52:43 CEST 2004

David MacQuigg <dmq at gain.com> wrote in 
news:gooob05i2k58fbkim5pe5mqm2qgsefcl7j at 4ax.com:

> Actually, __new__ is a staticmethod.

Thanks, I'd missed that.

> I haven't yet decided what to do with __new__ and __metaclass__.  I've
> received a good example of using these to generate classes for the
> Animals examples, but the Python documentation is poor, and I think I
> can accomplish the purpose of automatically generating classes with
> correct attributes, using "factory functions" as your example
> illustrates.
The main time I've felt a need to use __new__ is to implement flyweight 
classes. If the object to be returned is based entirely on the arguments to 
the constructor, and the object doesn't contain modifiable state, then you 
can cache the objects you have already created and simply return the 
appropriate one from the cache instead of creating a new object.

The same argument would apply to singletons except I've never yet found a 
good use for a singleton :-)

More information about the Python-list mailing list