Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9612 > unrolled thread

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

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2011-07-16 18:35 +1000
Last post2011-07-23 14:09 -0700
Articles 7 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Is there a way to customise math.sqrt(x) for some x? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-16 18:35 +1000
    Re: Is there a way to customise math.sqrt(x) for some x? Chris Angelico <rosuav@gmail.com> - 2011-07-16 19:14 +1000
      Re: Is there a way to customise math.sqrt(x) for some x? Nobody <nobody@nowhere.com> - 2011-07-16 14:42 +0100
      Re: Is there a way to customise math.sqrt(x) for some x? John Nagle <nagle@animats.com> - 2011-07-23 08:31 -0700
    Re: Is there a way to customise math.sqrt(x) for some x? Peter Otten <__peter__@web.de> - 2011-07-16 11:37 +0200
    Re: Is there a way to customise math.sqrt(x) for some x? Gary Herron <gherron@islandtraining.com> - 2011-07-16 08:56 -0700
    Re: Is there a way to customise math.sqrt(x) for some x? rantingrick <rantingrick@gmail.com> - 2011-07-23 14:09 -0700

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

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-07-16 18:35 +1000
SubjectIs there a way to customise math.sqrt(x) for some x?
Message-ID<4e214d5a$0$29975$c3e8da3$5496439d@news.astraweb.com>
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.


-- 
Steven

[toc] | [next] | [standalone]


#9613

FromChris Angelico <rosuav@gmail.com>
Date2011-07-16 19:14 +1000
Message-ID<mailman.1106.1310807690.1164.python-list@python.org>
In reply to#9612
On Sat, Jul 16, 2011 at 6:35 PM, Steven D'Aprano
<steve+comp.lang.python@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

[toc] | [prev] | [next] | [standalone]


#9621

FromNobody <nobody@nowhere.com>
Date2011-07-16 14:42 +0100
Message-ID<pan.2011.07.16.13.42.35.248000@nowhere.com>
In reply to#9613
On Sat, 16 Jul 2011 19:14:47 +1000, Chris Angelico wrote:

> Only thing I can think of is:
> 
> import math
> math.sqrt=lambda(x) x.__sqrt__(x) if x.whatever else math.sqrt(x)

math.sqrt=(lambda sqrt=math.sqrt: lambda x: x.__sqrt__(x) if hasattr(x,'__sqrt__') else sqrt(x))()

[toc] | [prev] | [next] | [standalone]


#10175

FromJohn Nagle <nagle@animats.com>
Date2011-07-23 08:31 -0700
Message-ID<4e2ae93a$0$2159$742ec2ed@news.sonic.net>
In reply to#9613
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@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

[toc] | [prev] | [next] | [standalone]


#9614

FromPeter Otten <__peter__@web.de>
Date2011-07-16 11:37 +0200
Message-ID<mailman.1107.1310809049.1164.python-list@python.org>
In reply to#9612
Steven D'Aprano 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.

There seem to be only three methods that can be customized:

$ grep 'LookupSpecial' mathmodule.c
    method = _PyObject_LookupSpecial(number, "__ceil__", &ceil_str);
    method = _PyObject_LookupSpecial(number, "__floor__", &floor_str);
    trunc = _PyObject_LookupSpecial(number, "__trunc__", &trunc_str);

[toc] | [prev] | [next] | [standalone]


#9626

FromGary Herron <gherron@islandtraining.com>
Date2011-07-16 08:56 -0700
Message-ID<mailman.1112.1310832355.1164.python-list@python.org>
In reply to#9612
On 07/16/2011 01:35 AM, Steven D'Aprano 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.
>
>
Create a file named myMath.py containing:
--------------------------------------
from math import *

def sqrt(x):
     ...whatever you want...
---------------------------------------

Then you can:
   import math #to get normal stuff and
   import myMath # to get your customized stuff
or
   import myMath as math # to get your stuff but named math.



Gary Herron

[toc] | [prev] | [next] | [standalone]


#10178

Fromrantingrick <rantingrick@gmail.com>
Date2011-07-23 14:09 -0700
Message-ID<dcff1ca1-c5b0-4d7c-a59e-06a6fd72efbe@10g2000yqn.googlegroups.com>
In reply to#9612
On Jul 16, 3:35 am, Steven D'Aprano <steve
+comp.lang.pyt...@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.

Hmm, this question should win ambiguous award of the year. Here, allow
me to answer by proxy.

----------------------------------------
 Ambiguos Caller Sketch
----------------------------------------
SCENE: (A car owner calls a custom body shop to request work on his
car)
SHOP: "Custom Car Works, how may we help you?"
OWNER: Hello sir. I would like to customize my car.
SHOP: Sure. What kind of car is it exactly?
OWNER: Well i would say it's a normal car.
SHOP: Okay. What size engine does it have?
OWNER: I would say it's a fairy large engine.
SHOP: Interesting. So what kind of work are you needing?
OWNER: Something custom because i want it to look very "customized".
SHOP: Well Sir we are after all a CUSTOM shop! *rolls-eyes*
OWNER: Great, because i need some custom work!
SHOP: Okay Sir, would you like us to PAINT your car?
OWNER: Sure, that would be great.
SHOP: Okay, so what color should we PAINT your car?
OWNER: A light color would be fine.
SHOP: I see. I think we can handle this job for you just fine however
I just have one more question.
OWNER: Yes...
SHOP: Tell me, does this sound like a phone hanging up? *click*
OWNER: Yes.
...
OWNER: Hello?
...
OWNER: Are you there?
----------------------------------------

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web