如何在模块中重载sqrt? [英] how to overload sqrt in a module?

查看:55
本文介绍了如何在模块中重载sqrt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个模块,提供一些数学函数

除了在math,cmath等中定义的那些之外,但是我想把它做成

work与任何一起重载数学函数的类型。


因此,我想写一下:


module_f.py

----

def f(x):

"""计算sqrt of x""

return sqrt(x)

来自math import *
import module_f
module_f.f(3)
Traceback(最近一次调用最后一次):

文件"< stdin>" ,第1行,在?

文件" module_f.py",第2行,在f

返回sqrt(x)

NameError:全局名称''sqrt''未定义


我理解为什么sqrt不在范围内,但我的问题是:

最好的方法是什么这样做吗?


这是一个相当难看的解决方案:


module_g.py

----- ------

def g(x,math):

返回math.sqrt(x)

import math,cmath, module_g
module_g.g(2,math)
1.4142135623730951 module_g.g(-2,cmath)



1.4142135623730951j


我相信有更好的方法可以做到这一点t使用

类型的参数(动态范围将解决

问题,但我们不想去那里......)。请注意以下

函数可以正常工作


def f(x):

返回abs(x)


因为附加了该类型的特殊成员__abs__。对于sqrt,sin等没有

对应的成员。


什么是正确的? pythonic方式这样做?


Michael。

解决方案

Michael McNeil Forbes写道:

我想编写一个模块,提供一些数学函数,除了数学,cmath等定义的那些之外,但我想使它与any一起工作。重载数学函数的类型。

因此,我想写一下:

module_f.py
----
def f(x ):
"""计算sqrt of x"""
返回sqrt(x)

来自math import *
import module_f
module_f.f(3)



Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
文件" module_f.py",第2行,在f
返回sqrt(x)
NameError:全局名称''sqrt''未定义

我理解为什么sqrt不在范围内,但我的问题是:最好的方法是什么?




你需要导入数学在使用它的模块中。一个

模块中的进口不会(通常)影响另一个模块中可用的变量。


module_f.py

----

来自数学导入sqrt#比数学导入更明确*

def f(x):

"" ;"计算sqrt of x""

返回sqrt(x)


Kent


>>我想编写一个模块,在

在math,cmath等中定义的顶部提供一些数学函数但是我想让它工作
withany重载数学函数的类型。

因此,我想写一下:

module_f.py
----
def f(x ):
"""计算sqrt of x""
return sqrt(x)

>来自数学导入*
> import module_f
> module_f.f(3)



Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
文件中" module_f.py",第2行,在f
返回sqrt(x)
NameError:全局名称''sqrt''未定义

我理解为什么sqrt是不在范围内,但我的问题是:最好的方法是什么?



你需要在使用它的模块中导入数学。一个模块中的导入
不会(通常)影响另一个模块中可用的变量。

module_f.py
----
来自math import sqrt#比数学导入更明确*
def f(x):
"""计算sqrt of x""
返回sqrt(x)

肯特




我知道,但重点是我想允许模块的用户

能够指定应该使用哪个sqrt函数,具体取决于类型

of x。在模块中导入数学将阻止用户使用复杂类型的f()

(或另一个示例的尺寸类型)。


这就是为什么我提出了丑陋但功能性的解决方案


module_g.py

-----------

def g(x,a_math):

"""""使用a_math.sqrt计算x的sqrt)

返回a_math.sqrt(x)


用户至少可以指定使用cmath而不是数学:

import cmath ,module_g
module_g.g(8j,cmath)



(2 + 2j)


Michael。


Michael McNeil Forbes写道:

这是一个相当难看的解决方案:

module_g.py
-----------
def g(x,math):
返回math.sqrt(x)

< blockquote class =post_quotes>

import math,cmath,module_g
module_g.g(2,数学)
1.4142135623730951
module_g.g(-2,cmath)



1.4142135623730951j

我相信有更好的方法可以使用
类型的参数(动态范围可以解决这个问题)问题,但我们不想去那里......)。注意以下
函数可以正常工作

def f(x):
返回abs(x)

因为附加了特殊成员__abs__到类型。对于sqrt,sin等没有
相应的成员。

什么是适当的? pythonic方法这样做?




使用已经实现了你想要的功能的相应库。


在[8]中:A类(对象):

...:def __init __(self,x):

...:self.x = x

...:def sqrt(self):

...:返回2 * self.x

......:

...:


在[9]中:a = A(10)


在[10]中:import numpy


在[11]中:numpy.sqrt(a)

Out [11]:20


在[12]中: numpy.sqrt(10)

Out [12]:3.1622776601683795


在[13]中:numpy.sqrt(10j)

Out [13] :( 2.2360679774997898 + 2.2360679774997898j)


-

Robert Kern
ro ********* @ gmail.com


在地狱的领域草长高的地方

是否允许死亡的坟墓死亡。

- 理查德·哈特


I would like to write a module that provides some mathematical functions
on top of those defined in math, cmath etc. but I would like to make it
work with "any" type that overloads the math functions.

Thus, I would like to write:

module_f.py
----
def f(x):
""" Compute sqrt of x """
return sqrt(x)

from math import *
import module_f
module_f.f(3) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "module_f.py", line 2, in f
return sqrt(x)
NameError: global name ''sqrt'' is not defined

I understand why sqrt is not in scope, but my question is: what is the
best way to do this?

Here is one fairly ugly solution:

module_g.py
-----------
def g(x,math):
return math.sqrt(x)
import math, cmath, module_g
module_g.g(2,math) 1.4142135623730951 module_g.g(-2,cmath)


1.4142135623730951j

I am sure there is a better way of doing this that makes use of the
type of the argument (Dynamical scoping would solve the
problem but we don''t want to go there...). Note that the following
function would work fine

def f(x):
return abs(x)

because of the special member __abs__ attached to the type. There is no
corresponding member for sqrt, sin etc.

What is the "proper" pythonic way to do this?

Michael.

解决方案

Michael McNeil Forbes wrote:

I would like to write a module that provides some mathematical functions
on top of those defined in math, cmath etc. but I would like to make it
work with "any" type that overloads the math functions.

Thus, I would like to write:

module_f.py
----
def f(x):
""" Compute sqrt of x """
return sqrt(x)

from math import *
import module_f
module_f.f(3)



Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "module_f.py", line 2, in f
return sqrt(x)
NameError: global name ''sqrt'' is not defined

I understand why sqrt is not in scope, but my question is: what is the
best way to do this?



You need to import math in the module that uses it. Imports in one
module don''t (in general) affect the variables available in another module.

module_f.py
----
from math import sqrt # more explicit than from math import *
def f(x):
""" Compute sqrt of x """
return sqrt(x)

Kent


>> I would like to write a module that provides some mathematical functions on

top of those defined in math, cmath etc. but I would like to make it work
with "any" type that overloads the math functions.

Thus, I would like to write:

module_f.py
----
def f(x):
""" Compute sqrt of x """
return sqrt(x)

> from math import *
> import module_f
> module_f.f(3)



Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "module_f.py", line 2, in f
return sqrt(x)
NameError: global name ''sqrt'' is not defined

I understand why sqrt is not in scope, but my question is: what is the best
way to do this?



You need to import math in the module that uses it. Imports in one module
don''t (in general) affect the variables available in another module.

module_f.py
----
from math import sqrt # more explicit than from math import *
def f(x):
""" Compute sqrt of x """
return sqrt(x)

Kent



I know, but the point is that I want to allow the user of the module to be
able to specify which sqrt function should be used depending on the type
of x. Importing math in the module would prevent the user from using f()
with complex types (or dimensioned types for another example).

That is why I suggested the ugly, but functional solution

module_g.py
-----------
def g(x,a_math):
"""Compute sqrt of x using a_math.sqrt)
return a_math.sqrt(x)

The user can at least specify to use cmath instead of math:

import cmath, module_g
module_g.g(8j,cmath)


(2+2j)

Michael.


Michael McNeil Forbes wrote:

Here is one fairly ugly solution:

module_g.py
-----------
def g(x,math):
return math.sqrt(x)

import math, cmath, module_g
module_g.g(2,math)
1.4142135623730951
module_g.g(-2,cmath)



1.4142135623730951j

I am sure there is a better way of doing this that makes use of the
type of the argument (Dynamical scoping would solve the
problem but we don''t want to go there...). Note that the following
function would work fine

def f(x):
return abs(x)

because of the special member __abs__ attached to the type. There is no
corresponding member for sqrt, sin etc.

What is the "proper" pythonic way to do this?



Use the appropriate library which already implements the features you want.

In [8]: class A(object):
...: def __init__(self, x):
...: self.x = x
...: def sqrt(self):
...: return 2*self.x
...:
...:

In [9]: a = A(10)

In [10]: import numpy

In [11]: numpy.sqrt(a)
Out[11]: 20

In [12]: numpy.sqrt(10)
Out[12]: 3.1622776601683795

In [13]: numpy.sqrt(10j)
Out[13]: (2.2360679774997898+2.2360679774997898j)

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


这篇关于如何在模块中重载sqrt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆