Decorators: an outsider's perspective

Anthony Baxter anthonybaxter at gmail.com
Mon Aug 16 11:24:19 EDT 2004


On Mon, 16 Aug 2004 06:20:18 -0400, Paul Morrow <pm_mon at yahoo.com> wrote:
> > Python currently doesn't _care_ about the argument list, and the variable
> > names in it.
> >
> 
> One thought is, to preserve backwards compatability, this special
> behavior would only be invoked for subclasses of the new Object class.

How is this backwards compatible? If you'd introduced it at the same time
as object, _maybe_ you could argue that. But this isn't the case here.

> > Adding this sort of behaviour is nasty. How, for instance, would you handle
> > adding a method to a class at runtime? Do you poke into the newly added
> > method to make sure you get it right? Do you not do this?
> Same rules apply.  The Python system would dynamically determine the
> method's type when you add it (at runtime), so you'd need to make sure
> that the first parm name is correct in advance.

Really. And how would you implement this special casing in a way that
wasn't incredibly tricky to explain to users running across it? One of the
great things about Python is that if you need to, or you want to, you can
poke into the innards of the OO system and see how it works (particularly
since descriptors were added). This adds Perl-like levels of OO-hackery.
If that comment is unclear, I mean that when you poke into Perl's OO,
it becomes very clear, very quickly, that it's a hack.

> > What about for more complex decorators? Remember, staticmethod and
> > classmethod are the trivial cases that this feature addresses. How do you
> > handle the more complex ones?
> The point is that method type declarations are not needed, so decorators
> wouldn't be concerned with this.  Take them off the list of things
> decorators can do.

Huh? You proposed a hack to specify class and static methods, using a 
custom meta-class that pokes at the argument list of a function, and 
suddenly you've removed the need for decorators for this? Please - feel 
free to use your metaclass in your own code, but  don't think that this is 
in any way a solution that should be considered for "mainstream" python 
use. It's a hack - a neat hack and a nice example of some of the things
metaclasses can do[1], but it's not a valid "solution" for the problem that
decorators are meant to solve. Look, staticmethod and classmethod are
a tiny tiny fraction of the things that decorator will be used for. Looking 
at a large amount of Python code I've got here, I can see three examples
of classmethods (all alternate constructors) and no uses of staticmethod
at all.

Anthony

([1] and, I should note, one I was about to post until I saw your post <wink>)



More information about the Python-list mailing list