I have a python object that conceptually allows access to an array full of strings through iterators and getters. However, since calculating the exact value of each element in the array is really expensive, I am looking into returning a proxy object for the content of each slot in the array and then calculate on the fly the actual value when it is really needed.
Namely, I would like to write this:
bar = foo.get(10) # just returns a proxy
baz = bar # increase proxy reference
l = [baz] # actually increase proxy reference again.
print baz # ooh, actually need the value. Calculate it only the fly.
v = '%s' % bar # I need the value here again
if bar is None: # I need the value here again
print 'x'
if bar: # I need the value here again
print 'x'
for i in bar: # I need the value here again
print i
In C++, I would try to overload the dereferencing operator… Any idea ?
I understand that for each of these cases, I could overload specific python ‘magic’ functions (such as __str__ for print baz) but I wonder if:
- this is going to actually cover all possible usecases (are there ways to access the content of a variable that does not involve using a python magic function)
- there is a more generic way to do this
In python you’d return a custom type, and override the
__str__()method to calculate the string representation at printing time.Depending on your use-cases, you are still looking at the various hooks python provides:
__getattr__method, or by using aproperty.__getitem__.You’ll have to decide, based on your use-case, what you need to hook into, at which point it becomes inevitable that you need to make the expensive calculation. Python will let you hook that almost any point in an object’s lifetime with ease.