strange __call__

Steven Bethard steven.bethard at gmail.com
Wed Jun 29 17:28:03 EDT 2005


Steven Bethard wrote:
> 
> def wrap(obj):
>      def f(*args, **kwargs):
>          for arg in args:
>              print arg
>          return obj(*args, **kwargs)
>      return f
> 
> @wrap
> def func(a, b, c):
>      ...
> 
> class C(object):
>      ...
> C = wrap(C)

Rahul top-posted:
 > If you do C = wrap(C) C no longer remains a class..it becomes a
 > function.

And if you do
     func = wrap(func)
which is the equivalent of
     @wrap
     def func(...):
         ...
then func no longer has the same signature.  But as Reinhold suggests, 
does that really matter?  In the case of the class, you can still call 
it to create class instances.  In the case of the function, you can 
still call it to retrieve return values.  Why do you care about the type 
of the object?

In the case that it does matter, e.g. you want to be able to invoke your 
methods from the class instead of the instance, you can wrap the 
specific function that you need wrapped, e.g.

class C(object):
     @wrap
     def __new__(cls, *args):
         super(C, cls).__new__(cls, *args)
     ...

STeVe



More information about the Python-list mailing list