何时在python中使用运算符重载的经验法则 [英] Rules of thumb for when to use operator overloading in python

查看:69
本文介绍了何时在python中使用运算符重载的经验法则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我在C ++类中记得的内容,这位教授说运算符重载是很酷的,但是由于要花很多时间才能涵盖所有最终情况(例如,重载+时,您可能还想重载+++=,并确保处理最终情况,例如向其自身添加对象等),仅应在此功能会对代码产生重大影响的情况下考虑使用,例如重载数学应用程序中矩阵类的运算符.

From what I remember from my C++ class, the professor said that operator overloading is cool, but since it takes relatively a lot of thought and code to cover all end-cases (e.g. when overloading + you probably also want to overload ++ and +=, and also make sure to handle end cases like adding an object to itself etc.), you should only consider it in those cases where this feature will have a major impact on your code, like overloading the operators for the matrix class in a math application.

是否同样适用于python?您会建议在python中重写操作符行为吗?你能给我什么经验法则?

Does the same apply to python? Would you recommend overriding operator behavior in python? And what rules of thumb can you give me?

推荐答案

在创建要包含在现有抽象基类"(ABC)中的新类时,运算符重载最为有用-实际上,许多标准库模块集合中的ABC依赖于某些特殊方法的存在(以及一种特殊的方法(一种名称以双下划线(又称为"dunders")开头和结尾)的方法,正是您在Python中执行运算符重载的方式).这样可以提供良好的入门指导.

Operator overloading is mostly useful when you're making a new class that falls into an existing "Abstract Base Class" (ABC) -- indeed, many of the ABCs in standard library module collections rely on the presence of certain special methods (and special methods, one with names starting and ending with double underscores AKA "dunders", are exactly the way you perform operator overloading in Python). This provides good starting guidance.

例如,Container必须覆盖特殊方法__contains__,即成员资格检查运算符item in container(如if item in container:一样-不要与for语句for item in container:,它依赖于__iter__!-). 同样,Hashable必须覆盖__hash__Sized必须覆盖__len__SequenceMapping必须覆盖__getitem__,依此类推. (此外,ABC可以为您的班级提供混合功能-例如,SequenceMapping都可以基于您提供的__getitem__覆盖提供__contains__,从而自动将您的班级设为Container ).

For example, a Container class must override special method __contains__, i.e., the membership check operator item in container (as in, if item in container: -- don't confuse with the for statement, for item in container:, which relies on __iter__!-). Similarly, a Hashable must override __hash__, a Sized must override __len__, a Sequence or a Mapping must override __getitem__, and so forth. (Moreover, the ABCs can provide your class with mixin functionality -- e.g., both Sequence and Mapping can provide __contains__ on the basis of your supplied __getitem__ override, and thereby automatically make your class a Container).

除了collections之外,如果新类是一个数字",则通常需要重写特殊方法(即,提供运算符重载).还存在其他特殊情况,但是可以抵制重载运算符仅出于冷静"的诱惑,而没有正常"含义的语义联系,就像C ++的流对<<>>以及Python字符串所做的那样(在Python 2.*中,幸运的是不在3.*中了;-)为%做了-当此类运算符不再意味着移位"或除法余数"时,您只会引起混乱.语言的标准库可以摆脱它(尽管不应该;-),但是除非您的库像该语言的标准库一样普及,否则混淆会很麻烦!-)

Beyond the collections, you'll want to override special methods (i.e. provide for operator overloading) mostly if your new class "is a number". Other special cases exist, but resist the temptation of overloading operators "just for coolness", with no semantic connection to the "normal" meanings, as C++'s streams do for << and >> and Python strings (in Python 2.*, fortunately not in 3.* any more;-) do for % -- when such operators do not any more mean "bit-shifting" or "division remainder", you're just engendering confusion. A language's standard library can get away with it (though it shouldn't;-), but unless your library gets as widespread as the language's standard one, the confusion will hurt!-)

这篇关于何时在python中使用运算符重载的经验法则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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