operator overloading for __truediv__ in python

血红的双手。 提交于 2019-12-05 05:57:36

The object.__truediv__() special method is only used with the / operator and then only if you have switched the Python compiler to use true division with:

from __future__ import division

If you did not use that import, the / operator calls the object.__div__() special method if present.

The // operator on the other hand calls the object.__floordiv__() special method, which you did not implement.

From the docs:

object.__div__(self, other) 
object.__truediv__(self, other)

The division operator (/) is implemented by these methods. The __truediv__() method is used when __future__.division is in effect, otherwise __div__() is used. If only one of these two methods is defined, the object will not support division in the alternate context; TypeError will be raised instead.

And here:

A future statement is a directive to the compiler that a particular [python program] should be compiled using syntax or semantics that will be available in a ... future release of Python. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features before the release in which the feature becomes standard.

future_statement: from __future__ import feature

The features recognized by Python 2.x are unicode_literals, print_function, absolute_import, division, generators, nested_scopes and with_statement

Now, some tests:

~$ python2.7
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3/2
1
>>> exit()

~$ python3.2
Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3/2
1.5

So, you see, the effect of the / operator changed in python 3.x. You can see that in the example below, as well:

class Dog(object):
    def __div__(self, other):
        print("__div__ called")
    def __truediv__(self, other):
        print("__truediv__ called")


Dog() / Dog()

--output:--
~/python_programs$ python2.7 myprog.py 
__div__ called

~/python_programs$ python3.4 myprog.py
__truediv__ called

Because __truediv__ is not called by the / operator in python 2.x, overriding __truediv__ in python 2.x has no effect.

PEP 238 - PEP 238 -- Changing the Division Operator

We propose the following transitional measures:

    - Classic division will remain the default in the Python 2.x
      series; true division will be standard in Python 3.0.

    - The // operator will be available to request floor[, i.e. integer,] 
      division unambiguously.

    - The future division statement, spelled "from __future__ import
      division", will change the / operator to mean true division
      throughout the [program]

Now, look what happens here:

from __future__ import division

class Dog(object):
    def __div__(self, other):
        print("__div__ called")
    def __truediv__(self, other):
        print("__truediv__ called")


Dog() / Dog()

--output:--
~/python_programs$ python2.7 myprog.py 
__truediv__ called

~/python_programs$ python3.4 myprog.py
__truediv__ called

Now, you get the python3.x effect for the / operator in python 2.x. So, now you can override __truediv__ to make the / operator do what you want.

Note that if you want integer division in python 3.x, i.e. 3/2 => 1, then you have to use the // operator, which is implemented by __floordiv__. Likewise, if you do from __future__ import division in python 2.x, then to get integer division, you have to use the // operator; and if you want to override the // operator in a class, you need to implement __floordiv__.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!