Wrapping Python Objects int / float

I am working on a Python concurrency infrastructure (I know, YACF) and would like to be able to return variables as futures, but without the user noticing this.

Now I am doing:

x = someAsyncMethod(args)
print "Return value is %d" % x.get_value( )

Since the method returns a Future object, but I would like to:

x = someAsyncMethod(args)
print "Return value is %d" % x

But still .get_value () from x matters. Therefore, I would like to proxy Python objects, including int. Something like __get__, but so if ProxyInt was a proxy for int, and I did:

x = ProxyInt(10)
print x

or

n = x

My "__get__" method will be called and do the magic before doing "return 10"

+3
source share
2 answers
>>> class I(object):
...   def __int__(self):
...     return 42
... 
>>> i = I()
>>> print '%d' % i
42
>>> class F(object):
...   def __float__(self):
...     return 3.14
... 
>>> f = F()
>>> print '%f' % f
3.140000
>>> class S(object):
...   def __str__(self):
...     return 'foo'
... 
>>> s = S()
>>> print s
foo
+1
source

__getattr__ __getattribute__ ( , ) "", , -.

0

All Articles