DISCLAIMER: this should not be used in any serious code, I wrote it just for fun and do not even consider it smart.
Not sure if this is what you wanted (so forgive me if itβs off topic), but I really enjoyed it by creating it.
class FancyThing(object): def __init__(self, value): self.value = value def update(self, callback): self.value = callback(self.value) def __get__(self, instance, owner): return instance.value def __set__(self, instance, value): instance.value = value def __str__(self): return str(self.value) def __add__(self, other): return self.value + other
If you wrap something in this class, you can update with any random callback.
def some_func(val): return val * 3 a = FancyThing(3.5) print a a.update(tester) print a b = a + 5 print b
Outputs:
3.5 10.5 15.5
The funny thing is that you have to define many built-in methods in order to be able to use the internal value, as usual, for example, for __add__() .
source share