[Python-Dev] Class Methods

Alex Martelli aleaxit at yahoo.com
Fri Apr 20 18:02:49 EDT 2001


"Guido van Rossum" <guido at digicool.com> wrote in message
news:mailman.987781890.19791.python-list at python.org...
> > Look into the motivation for the Prototype Pattern in the GOF
> > book, or even better in the discussion of this pattern in the
> > 'Design Pattern Smalltalk Companion' book.
>
> I imagine many of us (including yours truly :-) don't recall that in
> enough detail and/or don't have the book handy to look it up, so would
> you please do us a favor and explain this in a bit more detail?

The Prototype creational design pattern allows client-code, or
framework-code, to create objects by calling a 'clone' method
on suitable "prototype" objects (normally registered with some
appropriate registrar-object by the subsystem that does know
exactly what the creation-process entails), where "clone"'s
design intent is to ask the prototype object "make a new
object that starts out as a copy of yourself".


> > This pattern is not needed if classes are 'first class' objects.
>
> Depending on what you mean by the 'first class' phrase (which means
> something different to everyone), Python classes are already as first
> class as they get.  E.g. they are tangible objects and you can pass
> them around and store them in variables.

Registering any kind of object-factory objects is an alternative
to the use of .clone() and the "make one like yourself" design
intent thereof, and of course Python class objects may be
used as object factories in this way.  But any callable might
do just as well, as long as calling it returns an object that is
"suitable"; in Python, neither the registrar nor the framework
or client code that uses it needs to know whether class objects
or factory functions are being registered for this.


> > What I want to avoid is
> >
> >   class X(...):
> >       ....
> >   initialize(X)
>
> What would initialize(X) do that you can't write in the class body?

I'm not sure, maybe use the class object X itself?  That object
isn't available in the class body.  For example, initialize(X)
could among other things enregister class object X with any
appropriate registrars, as above.  But that may not be what
is meant, as "initialize" may be meant to imply that the
argument object is modified (and I don't understand why
one would need to avoid the example usage I proposed,
anyway).


Alex






More information about the Python-list mailing list