Python学习笔记(二十九)- 操作符重载(Operator Overloading)

1.您可以使用哪两个运算符重载方法来支持类中的迭代?
答:类可以通过定义(或继承)__getitem__或__iter__来支持迭代。在所有迭代上下文(iteration contexts)中,Python首先尝试使用__iter__,它返回一个支持带有__next__方法的迭代协议的对象:如果继承搜索没有找到__iter__,Python会回退到__getitem__索引方法,该方法被重复调用,而索引会连续增高。如果使用,yield语句可以自动创建__next__方法。

 

2.哪两个运算符重载方法处理打印,以及在什么情况下?
答:__str __和__repr__方法实现对象打印显示。前者由print和str内置函数调用;如果没有__str__,则后者由print和str调用,并且始终由repr内置,交互式响应和嵌套外观(nested appearances)调用。也就是说,__repr__在任何地方都使用,除了在定义__str__时使用print和str。 __str__通常用于用户友好的显示; __repr__提供额外的细节或如代码形式的对象。

 

3.如何拦截类中的切片操作?
答:切片由__getitem__索引方法捕获:它使用切片对象而不是简单的整数索引来调用,切片对象可以根据需要传递或检查。在Python 2.X中,__getslice __(在3.X中不存在)也可以用于两个限制切片。

 

4.如何在class中使用原地加法(addition)?【a += 2】
答:就地加法首先尝试__iadd__,然后使用__add__执行第二个赋值。所有二元运算符都适用相同的模式。 __radd__方法也适用于右侧的加法【2 + a】。

 

5.什么时候应该提供操作符重载?
答:当一个类很自然地需要匹配内置类型的接口或需要模拟时。例如,集合可能模仿序列或映射接口,并且可调用对象可以被编写与期望函数的API一起使用。您通常不应该实现表达式运算符(expression operators),如果它们不能自然地和有逻辑地映射到您的对象中,而是使用通常被命名的方法(method)以取而代之。

 

注:转载《Learning Python 5th Edition》[奥莱理]

1. What two operator overloading methods can you use to support iteration in your classes?
2. What two operator overloading methods handle printing, and in what contexts?
3. How can you intercept slice operations in a class?
4. How can you catch in-place addition in a class?
5. When should you provide operator overloading?


1. Classes can support iteration by defining (or inheriting) __getitem__ or __iter__. In all iteration contexts, Python tries to use __iter__ first, which returns an object that supports the iteration protocol with a __next__ method: if no __iter__ is found by inheritance search, Python falls back on the __getitem__ indexing method, which is called repeatedly, with successively higher indexes. If used, the yield statement can create the __next__ method automatically.
2. The __str__ and __repr__ methods implement object print displays. The former is called by the print and str built-in functions; the latter is called by print and str if there is no __str__, and always by the repr built-in, interactive echoes, and nested appearances. That is, __repr__ is used everywhere, except by print and str when a __str__ is defined. A __str__ is usually used for user-friendly displays; __repr__ gives extra details or the object's as-code form. 
3. Slicing is caught by the __getitem__ indexing method: it is called with a slice object, instead of a simple integer index, and slice objects may be passed on or inspected as needed. In Python 2.X, __getslice__ (defunct in 3.X) may be used for two-limit slices as well.
4. In-place addition tries __iadd__ first, and __add__ with an assignment second. The same pattern holds true for all binary operators. The __radd__ method is also available for right-side addition.
5. When a class naturally matches, or needs to emulate, a built-in type's interfaces. For example, collections might imitate sequence or mapping interfaces, and callables might be coded for use with an API that expects a function. You generally shouldn't implement expression operators if they don't naturally map to your objects naturally and logically, though—use normally named methods instead.

你可能感兴趣的:(Learning,Python,5th,Edition)