Some language proposals.

Jacek Generowicz jacek.generowicz at cern.ch
Fri Mar 5 04:15:55 EST 2004


Josiah Carlson <jcarlson at nospam.uci.edu> writes:

> [snip classes not behaving like class instances]
> 
> I see.  You are not happy with the fact that instances of some other
> class, with a __call__ method are not considered a bound instance
> method.  I'm sure you have thought of it, but I would wrap it.

Or, I could just use a closure as I advocated in the first place. It
would be shorter, clearer, less error-prone and (much) more efficient.

> take another function call, but really, what other functionality do
> you need that the below does not provide?
> 
> 
> Give the below a shot.
> 
>   - Josiah
> 
> class callee:
>      def __call__(s, self, *args):
>          print self
> 
> def instance(self, *args):
>      print self
> 
> def wrapper(self, *args):
>      print self
>      self._c(self, *args)
> 
> class caller:
>      pass
> 
> caller.instance_method = instance
> caller.instance_method2 = wrapper
> foo = caller()
> caller._c = callee()

Which is just a very long-winded and run-time inefficient way of
achieving the following

class caller: pass

def method2(self, *args):
    print self

caller.method2 = method2


Please remember that this all started because of a suggestion that
instances with __call__ are better alternatives to closures in just
about any situation in Python. (Please remember that _I_ disagree with
this.)

In other words, yes, I _could_ do this, but why would I want to, given
that much better alternatives exist ... even ones which do not use
closures (see Michael Hudson's suggestions upthread on how to do this
in Python 2.3 using types.UnboungMethodType and new.instancemethod,
for example).



More information about the Python-list mailing list