feature wish

Remco Gerlich scarblac at pino.selwerd.nl
Tue Mar 6 09:50:43 EST 2001


Dan Parisien <dan at eevolved.com> wrote in comp.lang.python:
> it seems all the special purpose overloadable methods are to provide type 
> emulation through classes
> e.g.
> __nonzero__
> __contains__
> __setitem__
> etc.
> 
> it would be cool if there was another method provided that allowed a 
> programmer to overload the value returned by an instance as well.

No; the instance *is* a value, it returns nothing. If you want something to
be a tuple, make it a tuple ;).

> class example:
>         def __init__(self, name, data):
>                 self.name = name
>                 self.data = data
> 
>         def __value__(self):
>                 return self.data
> 
> eg = example("my name", (1,2,3))
> 
> >>>print eg
> (1,2,3)
> 
> You could get to the instance behind the value by using a function like
> instance(eg)

You can make eg *look* like a tuple by providing __repr__ and __str__
functions that return (for instance) repr(self.data).

-- 
Remco Gerlich



More information about the Python-list mailing list