I’m not clear how to pose this question. If I did, I’d probably be a lot closer to a solution.. I need some insight into inheritance.
I want to make a custom subtype of float. But I want the instance of this subtype to re-evaluate its value before performing any of the normal float methods (__add__,__mul__, etc..). In this example it should multiply its value by the global FACTOR:
class FactorFloat(float):
# I don't think I want to do this:
## def __new__(self, value):
## return float.__new__(self, value)
def __init__(self, value):
float.__init__(self, value)
# Something important is missing..
# I want to do something with global FACTOR
# when any float method is called.
f = FactorFloat(3.)
FACTOR = 10.
print f # 30.0
print f-1 # 29.0
FACTOR = 2.
print f # 6.0
print f-1 # 5.0
This is a just a sanitized example that I think gets my point across. I’ll post a more complex “real” problem if necessary.
for:
EDIT:
In response to the question in the comment; making things slightly more general and automated, using a class decorator. I would not loop over
dir(baseclass), but instead would explicitly list the methods I wished to wrap. In the example below, I list them in the class variable_scale_methods.