Are decorators really that different from metaclasses...

Jess Austin jess.austin at gmail.com
Wed Aug 25 16:48:34 EDT 2004


Paul Morrow <pm_mon at yahoo.com> wrote in message news:<mailman.2346.1093432824.5135.python-list at python.org>...
>
> Not exactly, but it would include defining those.  One way to think 
> about metadata is that it includes all information *about* an object, 
> but no information *used by* the object.
> 
> If we were to have a conversation about this conversation, then we would 
> be having a "meta-conversation."  /metadata/ is data that describes 
> data.  metadata for a function would be data that describes the 
> function; data *about* the function as opposed to data *used by* the 
> function.  For example:

Here I think you're making an unwarranted assumption.  There is
currently nothing stopping an object from using information about
itself to do its job.  And this is sometimes a useful freedom.  As an
example, you might have subclasses whose only real purpose is to keep
track of their class and metadata, and have all functionality provided
by their superclass:

class supe(object):
    """this docstring is rarely frobnosticated"""
    def do_something_based_on_metadata():
        frobnosticate(self.__doc__)

class sub1(supe):
    """I am sub1"""

class sub2(supe):
    """I am sub2"""

Something like this could often be considered premature
objectification, but I've used it successfully.  Also, you'll notice
that these are classes rather than simple functions - I admit I've
never had a need to think about function metadata before.

However, I know that many people have a specific view of metadata and
don't really see how decorators fit in.  To be frank, I think pep318
shoehorned in metadata to pretend that there was a wider application
area for decoration than there really is.  The current quite minimal
support for metadata is sufficient for 99% of metadata use cases.  My
example has worked in Python for some time.  I know that decorators
would allow different behaviors to be arbitrarily assigned to various
pieces of metadata, but that doesn't seem like a big win for code
maintainability.  If anyone decided to support pep318 based solely on
a love of metadata, he made a mistake.

I'm intrigued by decorators anyway, and in no small part because it
seems that they should be able to completely replace metaclasses,
which are themselves totally unrelated to metadata.  In fact, that's
what at first prompted me to read a thread with the title, "Are
decorators really that different from metaclasses..."  Class
decorators wouldn't be so different from metaclasses, but J2 or even
@pie are much better syntaxes than the current metaclass syntax.

In another post in this thread Anthony Baxter said they were
different.  I'll respond directly to that post.

later,
Jess



More information about the Python-list mailing list