Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to overload an operator "/"

I recently start teaching myself game programming. Someone recommend me to start with Python and I got the book "Beginning game development with Python and Pygame: From novice to professional". I got to a part where they teach about Vectors and creating a Vector2 class. Everything was going well until I tried to overload the division operator. My code goes like this:

class Vector2(object):

  def __init__(self, x=0.0, y=0.0):
    self.x = x
    self.y = y

  def __str__(self):
    return "(%s, %s)"%(self.x, self.y)

  @classmethod
  def from_points(cls, P1, P2):
    return cls(P2[0] - P1[0], P2[1] - P1[1])

  def __add__(self,rhs):
    return Vector2(self.x + rhs.x, self.y + rhs.y)

  def __sub__(self,rhs):
    return Vector2(self.x - rhs.x, self.y - rhs.y)

  def __mul__(self, scalar):
    return Vector2( self.x*scalar, self.y*scalar)

  def __div__(self, scalar):
    return Vector2( self.x/scalar, self.y/scalar)

Now, when I tried to call the "/" operator, this shows up:

AB = Vector2(10.0,25.0)
print(AB)   # <<<<(10.0, 25.0)
v1 = AB + Vector2(20.,10.)
print(v1)   # <<<<(30.0, 35.0)
v2 = AB - Vector2(20.,10.)
print(v2)   # <<<<(-10.0, 15.0)
v3 = AB * 3
print(v3)   # <<<<(30.0, 75.0)
print(v3 / 3)
TypeError: unsupported operand type(s) for /: 'Vector2' and 'int'

This was all in Python 3.3 but if I run it with Python 2.7, everything works correctly. Where's the problem?

like image 634
darkwatcher5 Avatar asked Feb 18 '14 16:02

darkwatcher5


People also ask

What is the correct way to overload operator?

When a binary operator is overloaded the corresponding assignment operator, if any, must be explicitly overloaded. We can use the default equality operator in an overloaded implementation of the equality operator. A public or nested public reference type does not overload the equality operator.

Why we Cannot overload operators?

These operators cannot be overloaded because if we overload them it will make serious programming issues. For an example the sizeof operator returns the size of the object or datatype as an operand. This is evaluated by the compiler. It cannot be evaluated during runtime.

Which operator Cannot overload operator?

You cannot overload the following operators: . You cannot overload the preprocessor symbols # and ## . An operator function can be either a nonstatic member function, or a nonmember function with at least one parameter that has class, reference to class, enumeration, or reference to enumeration type.

Can we overload () operator in C++?

This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.


2 Answers

In Python 3.x, you need to overload the __floordiv__ and __truediv__ operators, not the __div__ operator. The former corresponds to the // operation (returns an integer) and the latter to / (returns a float).

like image 101
isedev Avatar answered Oct 04 '22 07:10

isedev


In Python 3, the division operators are called __truediv__ and __floordiv__. See the Data model documentation for more information.

like image 7
kwatford Avatar answered Oct 04 '22 07:10

kwatford