metaclass and customization with parameters

Carlos Ribeiro carribeiro at gmail.com
Mon Oct 4 09:30:55 EDT 2004


On 4 Oct 2004 03:23:32 -0700, Michele Simionato
<michele.simionato at gmail.com> wrote:
> Notice that there already valuable resources on this stuff.
> Unfortunately, after several minutes of searching on www.python.org
> I didn't find them (which may denote that something is wrong with me
> or with python.org). Search for new style classes,
> Shalab <last name which spelling I cannot remember> and may be
> even Dave Kuhlman <where the spelling is probably wrong>.

I've read all the tutorials and resources pointed there from the Wiki
as a starting point. All do a good job (some better than others) at
explaining a lot of the technical issues. In fact, I've read both
articles you and David Mertz have written, and found them great. The
standard documentation is unfortunately up to this standard -- I've
tried to find comprehensive references for it inside Python's
reference manuals to no avail. This is something that really needs to
be done there.

But the problem, in my particular case, was not with metaclasses per
se, but with understanding the 'big picture'. With all respect for
you, David Mertz, Alex Martelli, and all the others who helped to
guide me through this (steep) learning curve -- you've done a great
job, and I have to thank you all -- but most of the documentation
seems to start 'from the inside'; in other words, from the perspective
of someone that thoroughly understands how Python works, specially
regarding the handling of declarations (classes, modules and
functions), and that also knows what metaclasses are good for. For
people that come from a different background (such as me), some of the
explanations fail to 'ring a bell', which usually only happens after a
lot of practical attempts and failures.

This very thread, including the last comments from myself and Alex
Martelli, is specially illuminating in this respect. It took me a
while to figure out the obvious -- that there are not 'real' class
declarations in Python, at least as far as they do exist in C++ or
Delphi (the languages that I worked with before Python). (btw, I
really don't know Java, but I feel confident to say that Java is
probably in the C++ camp here also).

Figuring out the obvious tend to be a big barrier to the learning
process. For experienced writers, it tends to be a problem also,
because it's so obvious that one feels compelled to leave it out of
the explanation. But that's the issue here -- the fact that what I'm
freely calling 'class declarations' in Python are not really
declarations (in the same sense of C++, for example), are just
executable code like anything else; and that only as a cleverly
designed side effect (*) a new class (or module) is built out from the
results of the code that was executed.

(*) I beg your pardon, that's just a figure of mine that shows how do
people with different mental models understand it when they finally
'get' it.

I have developed my own mental model on this, and it tells me that
classes in Python are declared in a two-step process: code execution,
and class creation. As I said, this may be obvious to Python insiders
(obvious to the point where it's natural and doesn't deserve any
special explanation), but I assume that it isn't, given:

a) my own trouble to understand the full implications of it;
b) the type of questions that pop up in this group;
c) the surprised look that even old-time pythoneers do when they see
code such as the 'def __metaclass__(...):' inside a class, or similar
hacks.

It may be the case that, as I work more with metaclasses, I will tend
towards a more natural vision of the process, and some of the nuances
that are now relevant to me may become a thing of the past. But I
think that I'm not alone on this, and that is the real problem faced
by many newcomers to Python.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list