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

John Nagle nagle at animats.com
Sat Jul 23 17:31:06 CEST 2011


On 7/16/2011 2:14 AM, Chris Angelico wrote:
> 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

     Why use a lambda?  Just use a def.

     A lambda with an "if" is un-Pythonic.

					John Nagle




More information about the Python-list mailing list