[Python-Dev] PEP 318: Let's propose some useful built-in decorators (original) (raw)

Guido van Rossum guido at python.org
Fri Apr 2 15:32:16 EST 2004


Cool. And now that I have my pedantic hat on, I may as well go all out. First, why call it funcattrs, when staticmethod and classmethod are underscoreless?

Just for variety. :-)

We had a discussion about naming conventions recently, and I believe the outcome was that most folks like underscores in their names. OTOH if I was writing this just for me, I would indeed make them underscoreless.

Second, I know it is effectively the same, but shouldn't the .update line use vars(funcobj) instead of funcobj.dict? This is something that I am asked (often!) by my Python students. I use vars(obj) since it looks less magical.

No, vars() might return a copy. dict is the real thing (unless it isn't -- try updating the dict of a new-style class :-).

But perhaps the real implementation should use setattr() anyway -- some function attributes are not stored in the dict (like doc) and these should still be settable this way (if they are settable at all). And if they are not settable, this should raise an error rather than silently creating an inaccessible entry in dict.

So let's rephrase that class as:

class funcattrs(object): def init(self, **kwds): self.attrs = kwds def call(self, func): for name, value in self.attrs.iteritems(): setattr(func, name, value) return func

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list