Mangle function name with decorator?

andrew cooke andrew at acooke.org
Wed Mar 18 11:43:12 EDT 2009


Adam wrote:
> Hey, Cliff.  Thanks for sharing this idea.  Unfortunately, providing a
> way to actually call the method with the mangled name is relatively
> easy, and there are options there.  The real issue, to me, seems to be
> finding a way to prevent Python from eating all but the last version
> of a function definition in a class.  While decorators are a elegant
> and unintrusive approach, I don't believe that there is any way for a
> decorator to collection information in a data structure and then
> provide that data back to the class instance or the class's metaclass.

way back i suggested you look at the property decorator.  that does a very
similar job, in that it combines three different methods into one.  it
does that by using a class as decorator and accumulating the methods in
the class.  as a side effect/bonus it has a curious syntax (you could make
this extensible by overriding getattr on the decorator).  so you would end
up with something like:

class Foo:

  @GET or @multi_dispatch('GET') or @multi.GET or something...
  def foo(...):
    ...

  @foo.POST
  def foo_post(...):
    ...

  @foo.DELETE
  def foo_delete(...):
    ...

another idea, which i have never tried, is to look at the metaclass and
intercept duplicate names there (perhaps by substituting the class
dictionary.

you are trying to do very "deep" things that most people do not do with
python.  that does not mean that there are no solutions, just that you
have to find them yourself (especially with the decline of this
newsgroup).

andrew





More information about the Python-list mailing list