vkubicki
vkubicki

Reputation: 1124

Python builtin operator extension

I create a subclass of float called aF, which contains extra informations and methods. I need to create operations on this class, and in particular I need to be able to compute the product of a normal float with this aF.

How can I extend the * operator of the float, so that a float*an augmentedFloat returns an augmentedFloat that fits my needs ?

I know I have to write something like def float.__mul__() but I fear that I will delete the standard definition of the operator *, for a float.

Can you help me ?

Thanks

Upvotes: 2

Views: 233

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601649

Let's assume f is a float and af is an AugmentedFloat. To make both the multiplications af * f and f * af work, you have to overwrite the __mul__() and __rmul__() methods. You don't need to add any methods to the built-in float type -- that's not even possible in Python.

Upvotes: 2

Related Questions