[Python-Dev] Class decorators

Jim Jewett jimjjewett at gmail.com
Sat Apr 1 00:01:54 CEST 2006


Nick Coghlan wrote:

> [ much good, including the @instance decorator ]

> P.S.  If all you want is somewhere to store mutable
> state between invocations, you can always use the
> function's own attribute space

    >>> def f(): print "Hi world from %s!" % f

    >>> f()
    Hi world from <function f at 0x00AE90B0>!

Not really.  That assumes the expected name is (permanently) bound to
*this* function in this function's globals.  That's normally true, but
not always, so relying on it seems wrong.

    >>> f="a string"
    >>> g()
    Hi world from a string!

And of course, sometimes I really do want shared state, or helpers
(like other methods on a class), or one-time ininitialization plus
per-call parameters (like the send method on 2.5 generators), or ...

-jJ


More information about the Python-Dev mailing list