过载内置运算符 [英] overload builtin operator

查看:82
本文介绍了过载内置运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图在python中重载divide运算符以进行基本算术运算。

例如。 10/2 ......没有涉及课程。


我试图重新定义操作符.__ div__如下:


#my divide function

def safediv(a,b):

返回...


#重新分配buildin __div__

导入运算符

运算符.__ div__ = safediv


运算符.__ dict__似乎已更新OK但是''/''运算符仍然是

调用buildin __div__


有没有人知道这是否可行,如果我正在沿着正确的

路径进行上述尝试?

是否可以使用C扩展来执行此操作?


问候,

Shaun。

Hi,

I''m trying to overload the divide operator in python for basic arithmetic.
eg. 10/2 ... no classes involved.

I am attempting to redefine operator.__div__ as follows:

# my divide function
def safediv(a,b):
return ...

# reassign buildin __div__
import operator
operator.__div__ = safediv

The operator.__dict__ seems to be updated OK but the ''/'' operator still
calls buildin __div__

Does anyone know if this is possible and if I''m going along the correct
path with my attempts above?
Is it possible to do this using a C extention?

Regards,
Shaun.

推荐答案

Shaun写道:


我正在尝试在python中重载divide运算符以进行基本算术运算。
例如。 10/2 ......没有涉及课程。

我试图重新定义操作符.__ div__如下:

#my divide function
def safediv(a, b):
返回...

#reubign buildin __div__
导入运算符
运算符.__ div__ = safediv

运算符.__ dict__似乎更新好了,但''/''运算符仍然
调用buildin __div__


实际上,


int1 / int2


,我相信相当于


int .__ div __(int1,int2)


操作符.__ div__没有被调用。

有没有人知道这是否可行,如果我在上面的尝试中走正确的路径?
是吗?使用C扩展可以做到这一点吗?
Hi,

I''m trying to overload the divide operator in python for basic arithmetic.
eg. 10/2 ... no classes involved.

I am attempting to redefine operator.__div__ as follows:

# my divide function
def safediv(a,b):
return ...

# reassign buildin __div__
import operator
operator.__div__ = safediv

The operator.__dict__ seems to be updated OK but the ''/'' operator still
calls buildin __div__
Actually,

int1 / int2

is, I believe, equivalent to

int.__div__(int1, int2)

operator.__div__ does not get called.
Does anyone know if this is possible and if I''m going along the correct
path with my attempts above?
Is it possible to do this using a C extention?




不,你不能替换内置类型的方法。


-

Robert Kern
rk***@ucsd.edu
<无线电通信/>
在地狱的地方,草地长得很高

梦想的坟墓是否已经死亡。

- Richard Harter



No, you can''t replace the methods on builtin types.

--
Robert Kern
rk***@ucsd.edu

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


Shaun写道:


我正试图超越除法运算符用于基本算术的python。
例如。 10/2 ......没有涉及课程。

我试图重新定义操作符.__ div__如下:

#my divide function
def safediv(a, b):
返回...

#reubign buildin __div__
导入运算符
运算符.__ div__ = safediv

运算符.__ dict__似乎是更新好了,但''/''运算符仍然是
调用buildin __div __
Hi,

I''m trying to overload the divide operator in python for basic arithmetic.
eg. 10/2 ... no classes involved.

I am attempting to redefine operator.__div__ as follows:

# my divide function
def safediv(a,b):
return ...

# reassign buildin __div__
import operator
operator.__div__ = safediv

The operator.__dict__ seems to be updated OK but the ''/'' operator still
calls buildin __div__




它不会那样工作。您不能全局修改操作员的行为,

但您可以自定义操作员如何为您的类型工作。


考虑:


class safeint(int):

def __div __(self,other):

返回safediv(self,other)

>
safeint(10)/ 2

Reinhold



It won''t work that way. You cannot globally modify the behaviour of an operator,
but you can customize how an operator works for your type.

Consider:

class safeint(int):
def __div__(self, other):
return safediv(self, other)

safeint(10)/2

Reinhold


2005年8月25日星期四16:12:20 +0200,Reinhold Birkenfeld< re ************************ @ wolke7.net>写道:
On Thu, 25 Aug 2005 16:12:20 +0200, Reinhold Birkenfeld <re************************@wolke7.net> wrote:
Shaun写道:


我正在尝试在python中重载divide运算符以进行基本算术运算。
例如。 10/2 ......没有涉及课程。

我试图重新定义操作符.__ div__如下:

#my divide function
def safediv(a, b):
返回...

#reubign buildin __div__
导入运算符
运算符.__ div__ = safediv

运算符.__ dict__似乎是更新好但是''/''运算符仍然调用buildin __div __
Hi,

I''m trying to overload the divide operator in python for basic arithmetic.
eg. 10/2 ... no classes involved.

I am attempting to redefine operator.__div__ as follows:

# my divide function
def safediv(a,b):
return ...

# reassign buildin __div__
import operator
operator.__div__ = safediv

The operator.__dict__ seems to be updated OK but the ''/'' operator still
calls buildin __div__



它不会那样工作。您不能全局修改操作员的行为,
但您可以自定义操作员的类型。

考虑:

类safeint(int):
def __div __(自我,其他):
返回safediv(自我,其他)

safeint(10)/ 2



It won''t work that way. You cannot globally modify the behaviour of an operator,
but you can customize how an operator works for your type.

Consider:

class safeint(int):
def __div__(self, other):
return safediv(self, other)

safeint(10)/2



你是的,你不能全局修改操作员的行为,这似乎是OP想象的,但有一些麻烦,我认为可能会干扰
干扰将''< numerator-term> /< denominator-term>''翻译为

''safediv(< numerator-term>,< denominator-term>)' '通过在自定义的

导入过程中转换AST,这样无论在何处找到Div节点,都会替换CallFunc节点。例如,


这样的节点如


Div((名称('''分子''),姓名(''分母'') ))


替换另一个节点,如


CallFunc(名称(''safediv''),[名称('''分子'') ,名称(''分母'')],无,无)


其中后者的名称(''分子'')和名称(''分母'')是实际上是B节点'.left和.right的
,以便在相同的args上操作(这可以是表示子树的abitrary表示)。 />

当然,您可能还想更换


AugAssign(名称(''''),'/ ='',名称(''b''))


with


分配([AssName('''',''OP_ASSIGN'') ],CallFunc(名称(''safediv''),[名称(''''),名称(''b'')],无,无))


不幸的是,AST树似乎没有被设计为易于编辑y wrt

_replacing_节点通过走平板列表而不仅仅是按顺序找到它们。


我想我可以创建一个可以替换它的特殊步行器在任何地方找到的节点,以及

调用节点类型特定或默认提供的回调来为原始节点提供替换

传递给它们,但这需要一些专门的访问者等等,所以我稍后要回到

。但至少OP和你推动我重新考虑AST重写;-)

(也影响我的@@ sourcedeco想法;-)


问候,

Bengt Richter


You are right that you cannot globally modify the behaviour of an operator in the
sense the OP seems to be envisioning, but with some trouble I think it would be possible
to interfere with the translation of ''<numerator-term>/<denominator-term>'' to become
''safediv(<numerator-term>, <denominator-term>)'' by transforming the AST during a custom
import process, such that wherever a Div node is found, a CallFunc node is substituted. E.g.,

for a node like

Div((Name(''numerator''), Name(''denominator'')))

substitute another node like

CallFunc(Name(''safediv''), [Name(''numerator''), Name(''denominator'')], None, None)

where the Name(''numerator'') and Name(''denominator'') in the latter are actually
the Div node''s .left and .right, so as to operate on the same args (which can
be abitrary expression-representing subtrees).

Of course, you probably also want to replace

AugAssign(Name(''a''), ''/='', Name(''b''))

with

Assign([AssName(''a'', ''OP_ASSIGN'')], CallFunc(Name(''safediv''), [Name(''a''), Name(''b'')], None, None))

Unfortunately, the AST tree does not seem to be designed to be edited easily wrt
_replacing_ nodes found by walking via flattened lists vs. just _using_ them in order.

I think I can create special walker that can do replacements of nodes found anywhere, and
calling node-type-specific or default supplied callbacks to supply replacements for original nodes
passed to them, but this will require a number of specialized visitors etc., so I will have to get back
to this later. But at least the OP and you nudged me into thinking about AST rewriting again ;-)
(also affects my @@sourcedeco ideas ;-)

Regards,
Bengt Richter


这篇关于过载内置运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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