3

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

Right now I am doing:

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

Because the method return a Future object, but I would like it to be:

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

But still have the .get_value( ) of x invoked. Therefore I would like to Proxy wrap Python objects including int. Something like __get__, but so that if ProxyInt was a proxy for int and I did:

x = ProxyInt(10)
print x

or

n = x

My "__get__" method would be called and do some magic before doing "return 10"

renejsum
  • 69
  • 7
  • FYI: I believe the `__get__` method works with descriptors, but it doesn't do what you want here. The descriptor has to be an attribute of a class... i.e. you'd have to access the value through an attribute for `__get__` to work. – dappawit Mar 09 '11 at 23:14

2 Answers2

1
>>> 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
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

You could override __getattr__ or __getattribute__ (whichever suites your needs the best) to perform the "magic" on, for instance, the first attribute access of the proxy.

Nathan Davis
  • 5,636
  • 27
  • 39
  • Overriding these two would not work in my case, since I need a trigger for the access to the object itself, not just it's proprties. (But I use them for other things in the framework) – renejsum Mar 10 '11 at 07:00