PEP318: radical notion

Michael Sparks michaels at rd.bbc.co.uk
Wed Aug 25 15:33:34 CEST 2004


Arien Malec wrote:
...
> Is there a valid use case that supports arbitrary magical non-metadata
> transformations of standalone functions?

2 use cases (in addition to others people have mentioned) I can think of
off the top of my head:
   * Adding call tracing to specific functions without disturbing the
     code.
   * Wrapping generator functions so that they can recieve data in an
     IPC manner.

using:
   trace(level=1)
def somefunc(foo):
   if bla(foo) == 1:
       return weeble(foo)
   elif baz(foo) == 2:
       return wobble(foo)
   elif bibble(foo) == 3:
       return jeeble(foo)
   elif bloing(foo) == 4:
       return jobble(foo)
   elif nickynickynack(foo) == 5:
       return fnarkle(foo)

Adding a trace function in here temporarily would mean you can intercept
both the call and the return values conveniently - as you would with
memoise (say), but you can do something interesting with it.
Furthermore, if you're not sure your memoise decorator is doing the
right thing you could do something like:

using:
    trace(level=1)
    memoise
def somefunc(foo):
    if bla(foo) == 1: ...

Followed by...

using:
    memoise
    trace(level=1)
def somefunc(foo):
    if bla(foo) == 1: ...

And potentially:

using:
   trace(level=1)
def somefunc(foo):
   if bla(foo) == 1: ...

And do a compare and contrast of results. Behaviour is transformed,
there is no metadata involved and it's suitable for any function -
whether it's part of a class's namespace (as any kind of method), or
not. Since they're not necessarily part of a class you can't use
metaclasses to do this.

Regards,


Michael.
-- 
Michael.Sparks at rd.bbc.co.uk    
British Broadcasting Corporation, Research and Development
Kingswood Warren, Surrey KT20 6NP

This message (and any attachments) may contain personal views
which are not the views of the BBC unless specifically stated.





More information about the Python-list mailing list