Re: Some language proposals.
Michael Hudson <mwh@python.net > writes:
[color=blue]
> Jacek Generowicz <jacek.generowi cz@cern.ch> writes:
>[color=green]
> > Michael Hudson <mwh@python.net > writes:
> >[color=darkred]
> > > Jacek Generowicz <jacek.generowi cz@cern.ch> writes:[/color][/color][/color]
[color=blue]
> 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).[/color]
Yeeees, which is why waaaay upthread I wrote:
[color=blue][color=green][color=darkred]
> > > > 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.[/color][/color][/color]
(although I did misplace the terminology a little, I realize.)
[color=blue]
> 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.UnboundMe thodType(self, ob, cls)
>
> foo.inst = Callable()
>
> print foo.inst
> print foo().inst()
>
> (needs 2.3, for 2.2 use new.instancemet hod instead).[/color]
Aha !
I was doing this stuff way back in 2.2[*], where you get
[color=blue][color=green][color=darkred]
>>> print foo.inst[/color][/color][/color]
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.
[color=blue][color=green]
> > Aaah, this thread is an attempt to assimilate me :-) Now I understand.[/color]
>
> Damn, you noticed.[/color]
I'm well on the ball, I am.
Cheers,
[*] Actually, I'm still forced to use 2.2 in production for now.
Michael Hudson <mwh@python.net > writes:
[color=blue]
> Jacek Generowicz <jacek.generowi cz@cern.ch> writes:
>[color=green]
> > Michael Hudson <mwh@python.net > writes:
> >[color=darkred]
> > > Jacek Generowicz <jacek.generowi cz@cern.ch> writes:[/color][/color][/color]
[color=blue]
> 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).[/color]
Yeeees, which is why waaaay upthread I wrote:
[color=blue][color=green][color=darkred]
> > > > 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.[/color][/color][/color]
(although I did misplace the terminology a little, I realize.)
[color=blue]
> 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.UnboundMe thodType(self, ob, cls)
>
> foo.inst = Callable()
>
> print foo.inst
> print foo().inst()
>
> (needs 2.3, for 2.2 use new.instancemet hod instead).[/color]
Aha !
I was doing this stuff way back in 2.2[*], where you get
[color=blue][color=green][color=darkred]
>>> print foo.inst[/color][/color][/color]
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.
[color=blue][color=green]
> > Aaah, this thread is an attempt to assimilate me :-) Now I understand.[/color]
>
> Damn, you noticed.[/color]
I'm well on the ball, I am.
Cheers,
[*] Actually, I'm still forced to use 2.2 in production for now.
Comment