Issue
I'm converting a project (not originally mine) from python2
to python3
.
In one of the scripts I've:
sk = (key.Sub[0]/["point", ["_CM"]]).value
This works on py2
, but not on py3
, which throws an error:
unsupported operand type(s) for /: 'Primitive' and 'list'
Apart from the error, I'm also confused about the original syntax obj/list
.
Can you guys throw a light here?
Solution
This is due to the different behavior of the division operator between Python 2 and 3.
PS C:\Users\TigerhawkT3> py -2
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... def __div__(self, other):
... return 'call div'
... def __truediv__(self, other):
... return 'call truediv'
... def __floordiv__(self, other):
... return 'call floordiv'
...
>>> a = A()
>>> a/3
'call div'
>>> a//3
'call floordiv'
>>> exit()
PS C:\Users\TigerhawkT3> py
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... def __div__(self, other):
... return 'call div'
... def __truediv__(self, other):
... return 'call truediv'
... def __floordiv__(self, other):
... return 'call floordiv'
...
>>> a = A()
>>> a/3
'call truediv'
>>> a//3
'call floordiv'
You'll need to define the __truediv__
special method, rather than __div__
, for Python 3. See the data models for Python 2 and Python 3 for more info.
Answered By - TigerhawkT3
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.