possibly a dumb question
Hans Nowak
wurmy at earthlink.net
Sun Jun 30 00:19:10 EDT 2002
Adonis wrote:
> sure:
>
> class foo:
> def __init__(self, value):
> return value
> x = foo(0)
> print x ;yeilds 0
So you want to have an object, but use it like it has a certain value, like 0.
This is possible, but only to a certain extent. After x = foo(0), x will always
be a reference to the instance of foo (unles you rebind it of course). It will
not be a reference to the value you passed to the foo instance.
In the case of printing, you can define a __repr__ or __str__ method that
return the value rather than the boring <__main__.Foo instance at 0x008F9EC8>:
>>> class foo:
def __init__(self, value):
self.value = value
def __repr__(self):
return repr(self.value)
>>> f = foo(0)
>>> f
0
By defining other magic methods, you can do more, e.g. treat it like f was a
number, etc. This may, or may not, be what you're looking for.
If not, using f.value rather than f may be your best bet...
HTH,
--
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
More information about the Python-list
mailing list