__div__ not recognized automatically

A

Anton81

Hello!

I wrote a class

class NumX:
...
def __add__(self,other):
...
def __div__(self,other):
if not isinstance(other,NumX): other=NumX(other)
...

Somewhere else I use

a=(b+c)/2

where all variables are of NumX Type. When I execute the program it
complains that it can't find an operator "/" for "instance" and "integer".
However if I use pdb the same command works when started on the prompt. Also
the manual execution

a=(b+c).__div__(2)

works. Any suggestions what goes wrong?

Anton
 
S

Steven D'Aprano

When I execute the program it
complains that it can't find an operator "/" for "instance" and "integer".

How about if you post the actual traceback you get, rather than
paraphrasing? That way, we don't have to guess.
 
N

Nick Craig-Wood

Anton81 said:
class NumX:
...
def __add__(self,other):
...
def __div__(self,other):
if not isinstance(other,NumX): other=NumX(other)
...

Somewhere else I use

a=(b+c)/2

where all variables are of NumX Type. When I execute the program it
complains that it can't find an operator "/" for "instance" and "integer".
However if I use pdb the same command works when started on the prompt. Also
the manual execution

a=(b+c).__div__(2)

works. Any suggestions what goes wrong?

Post some code which actually demonstrates the problem.

Eg, change this code until it does demonstrate the problem

------------------------------------------------------------
class NumX(object):
def __init__(self, value):
self.value = long(value)

def __add__(self,other):
if not isinstance(other,NumX): other=NumX(other)
return NumX(self.value + other.value)

def __div__(self,other):
if not isinstance(other,NumX): other=NumX(other)
return NumX(self.value / other.value)

def __str__(self):
return "%s(%s)" % (self.__class__.__name__, self.value)

a = NumX(4)
b = NumX(2)

print a,b
print a+b
print (a+b)/2
 
P

Peter Otten

Anton81 said:
Hello!

I wrote a class

class NumX:
...
def __add__(self,other):
...
def __div__(self,other):
if not isinstance(other,NumX): other=NumX(other)
...

Somewhere else I use

a=(b+c)/2

where all variables are of NumX Type. When I execute the program it
complains that it can't find an operator "/" for "instance" and "integer".
However if I use pdb the same command works when started on the prompt.
Also the manual execution

a=(b+c).__div__(2)

works. Any suggestions what goes wrong?

If you have the

from __future__ import division

statement, you need to override __truediv__(), not __div__()

Peter
 
A

Anton81

If you have the
from __future__ import division

statement, you need to override __truediv__(), not __div__()

That worked after I also added
from __future__ import division
to all other modules I created.

Is it possible that there appears an inconsistency if the division is
imported in only some of the modules?

Anton
 
P

Peter Otten

Anton81 said:
That worked after I also added
from __future__ import division
to all other modules I created.

Is it possible that there appears an inconsistency if the division is
imported in only some of the modules?

Yes. If you use your class with and without the __future__ option you have
to implement both __div__ and __truediv__, I suppose.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top