Possible PEP: Improve classmethod/staticmethod syntax

Lulu of the Lotus-Eaters mertz at gnosis.cx
Wed Jun 4 06:18:51 CEST 2003


"Bryan" <belred1 at yahoo.com> wrote previously:
|playing symantec games here. from a programmers prespective, it looks
|and feels like a keyword.
|def myMethod(args) [staticmethod]:
|    blah, blah, blah
|def staticmethod myMethod(args):
|    blah, blah, blah
|these two "styles" appear to be the same, except the second one seems
|to be more natural and simpler and obvious to me.

FWIW, I also like the second one better.  But only slightly.

But I think you're missing something in both Aazh' and Kevin Smith's
suggestions.  'staticmethod' need not be some special transformation
deeply embedded in the Python interpreter.  Instead, staticmethod() can
be a regular, ordinary function object that takes one function as an
argument, and returns a slightly different function object.

The idea here is that it is possible to achieve something akin to what
metaclasses do.  I don't think staticmethod() or classmethod() could
themselves be written in pure Python rather than as C extensions (well,
maybe).  But you could also write your own function, and use it in a
metaclass-like way, e.g.:

    def fooDoc(f):
        f.__doc__ = "Bob's your uncle."
        return f

    class Klass(object):
        def meth(this, that) [fooDoc]:
            pass

or:

    class Klass(object):
        def fooDoc meth(this, that):
            pass

Now the method bound to the name Klass.meth is just like the one you
defined, only its docstring is set to the string "Bob's your uncle".
OK...  I bet someone can come up with a more *useful* example.  But
that's the idea I think both Aazh and Smith are suggesting.

Yours, Lulu...

--
 mertz@   _/_/_/_/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY:_/_/_/_/ v i
gnosis  _/_/                    Postmodern Enterprises         _/_/  s r
.cx    _/_/  MAKERS OF CHAOS....                              _/_/   i u
      _/_/_/_/_/ LOOK FOR IT IN A NEIGHBORHOOD NEAR YOU_/_/_/_/_/    g s






More information about the Python-list mailing list