Is there a way to customise math.sqrt(x) for some x?

Chris Angelico rosuav at gmail.com
Sat Jul 16 11:14:47 CEST 2011


On Sat, Jul 16, 2011 at 6:35 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> I have a custom object that customises the usual maths functions and
> operators, such as addition, multiplication, math.ceil etc.
>
> Is there a way to also customise math.sqrt? I don't think there is, but I
> may have missed something.

Only thing I can think of is:

import math
math.sqrt=lambda(x) x.__sqrt__(x) if x.whatever else math.sqrt(x)

I don't suppose there's a lambda version of try/catch?

ChrisA



More information about the Python-list mailing list