Possible PEP: Improve classmethod/staticmethod syntax

sismex01 at hebmex.com sismex01 at hebmex.com
Wed Jun 4 13:34:52 EDT 2003


> From: Lulu of the Lotus-Eaters [mailto:mertz at gnosis.cx]
> Sent: Wednesday, June 04, 2003 11:55 AM
> 
> Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote previously:
> |>     def fooDoc(f):
> |>         f.__doc__ = "Bob's your uncle."
> |>         return f
> |>     class Klass(object):
> |>         def meth(this, that) [fooDoc]:
> |>             pass
> |Or better:
> |
> |def Doc(docstring, **kw):
> |    def fDoc(f):
> |        f.__doc__ = docstring
> |        f.__dict__.update(kw)
> |        return f
> |class Klass(object):
> |    def meth(this, that) [Doc("Bob's your uncle.", Author="me")]:
> |        pass
> 
> I don't understand this version.  I see the idea that you want to pass
> arguments to the "function constructor"--but where does the name 'fDoc'
> come from?  How does 'Doc()' know to call 'fDoc()' (which I presume it
> wants to)?
> 
> Yours, Lulu...
>

It doesn't... I mean, Doc() doesn't call fDoc().  Doc() is a factory
function which produces functions which in turn are used to apply
transformations upon other functions.

So, in the declaration:

   def meth(this,that) [Doc("Bob's your uncle.", Author="me")]:
           pass

So, Doc(...) produces a function (fDoc) which applies decorations
to any function passed to it.  It'd be equivalent to:

   UncleBobFunc = Doc("Bob's your uncle.", Author="me")

   def meth(this,that) [UncleBobFunc]:
           pass

And, the transformation-application-mechanism inside the compiler,
internally, does something like:

   for transform in [UncleBobFunc]:
      meth = transform(meth)

for every transformation in the list.  That's why I like the list-like
notation best, it's very simple to implement in the compiler
machinery, very easy on the eyes, and simple to parse visually.

HTH!

-gca
Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.





More information about the Python-list mailing list