strange __call__

Andreas Kostyrka andreas at kostyrka.org
Wed Jun 29 05:40:27 EDT 2005


Just a guess, but setting "__X__" special methods won't work in most cases
because these are usually optimized when the class is created.

It might work if a.__call__ did exist before (because class a: contained
a __call__ definition).

Andreas

On Wed, Jun 29, 2005 at 09:15:45AM +0100, Michael Hoffman wrote:
> Rahul wrote:
> > Consider the following:
> > def a(x):
> >  return x+1
> > 
> > def b(f):
> >   def g(*args,**kwargs):
> >     for arg in args:
> >         print arg
> >     return f(*args,**kwargs)
> >   return g
> > 
> > a.__call__ = b(a.__call__)
> > 
> > now calling a(1) and a.__call__(1) yield 2 different results!!
> > i.e. for functions a(1) doesnt seem to be translated to a.__call__ if
> > you assign a new value to a.__call__?
> 
> I don't know why this happens, but setting the __call__ attribute of a 
> is a pretty strange thing to do. Why not just set a instead? The 
> original function a(x) will still be stored as a closure in what is 
> returned from b().
> 
> If this is of purely academic interest then the answer is I don't know. :)
> -- 
> Michael Hoffman
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list