Some language proposals.

Jacek Generowicz jacek.generowicz at cern.ch
Fri Feb 27 03:19:04 EST 2004


Michael Hudson <mwh at python.net> writes:

> Jacek Generowicz <jacek.generowicz at cern.ch> writes:
> 
> > Michael Hudson <mwh at python.net> writes:
> > 
> > > Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> Ah, ok.  To make it behave like a method, you need to make it a
> descriptor, i.e. implement __get__ (and make everything in sight
> new-style classes, of course).

Yeeees, which is why waaaay upthread I wrote:

> > > > If I were to implement them as instances then I'd have to
> > > > reimplement all the descriptors that take care of turning
> > > > functions into bound or unbound methods.

(although I did misplace the terminology a little, I realize.)

> import types
> 
> class foo(object):
>     pass
> 
> class Callable(object):
>     def __init__(self): # wonder why this is needed:
>         self.__name__ = 'Callable'
>     def __call__(self, ob):
>         return ob
>     def __get__(self, ob, cls=None):
>         return types.UnboundMethodType(self, ob, cls)
>     
> foo.inst = Callable()
> 
> print foo.inst
> print foo().inst()
> 
> (needs 2.3, for 2.2 use new.instancemethod instead).

Aha !

I was doing this stuff way back in 2.2[*], where you get

>>> print foo.inst
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 7, in __get__
TypeError: cannot create 'instance method' instances

but it does indeed work in 2.3. Thanks for pointing that out.

> > Aaah, this thread is an attempt to assimilate me :-)  Now I understand.
> 
> Damn, you noticed.

I'm well on the ball, I am.

Cheers,


[*] Actually, I'm still forced to use 2.2 in production for now.



More information about the Python-list mailing list