metaclass & __slots__

Alex Martelli aleax at aleax.it
Thu Jul 4 13:42:22 EDT 2002


On Thursday 04 July 2002 07:11 pm, Mark McEahern wrote:
> [Alex Martelli]
> [snip]
>
> > But, let's get into details:
>
> [snip]
>
> Alex, thank you for this very patient explanation of the fine details of
> Python class creation.  This helps me tremendously.

You're most welcome!  And, thanks right back for the feedback -- finding
out which explanatory "takes" on hard subjects work best in written form
is very helpful to me in determine how to write articles and book chapters
about those subjects.

I think the part that most needs to be fully explained and explored today
is WHY one would want to write customized metaclasses, rather than
HOW to write them (although the HOW is of course also necessary).  There
are, I think, few and somewhat marginal things that can ONLY be done with
a customized metaclass (e.g., determining what comes out when the class
object itself, rather than an instance, is printed).  Most metaclass tasks
could alternatively be handled with suitable __init__, __getattr__ &c special
methods.  But metaclasses can be simpler AND offer better performance.

Moreover, what complexity remains inevitable can better be moved into a
"framework", in such a way that a class-author does not need to worry all
the time about niggling details, but can just rely on framework-supplied
metaclasses to build the resulting class objects "just right", with whatever
tweaked semantics are necessary.

Still, until we get a good, solid tutorial set of examples comparing and
contrasting the metaclass-way and the classic-way to perform some such
set of tasks, I think this will remain rather hard to grasp in practice.  And
I'm still thinking about WHAT "good, solid tutorial set of examples" might
be most appropriate for this...


Alex





More information about the Python-list mailing list