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

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 func_attrs, 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