473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

__div__ not recognized automatically

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
Nov 2 '06 #1
6 1782
On Thu, 02 Nov 2006 12:59:32 +0100, Anton81 wrote:
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.
--
Steven.

Nov 2 '06 #2
Anton81 <us*****@anton.e4ward.comwrote:
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
------------------------------------------------------------

This prints

------------------------------------------------------------
NumX(4) NumX(2)
NumX(6)
NumX(3)
------------------------------------------------------------

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 2 '06 #3
Anton81 wrote:
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
Nov 2 '06 #4

Try
a=(b+c)/NumX(2)

TV

Anton81 wrote:
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
Nov 2 '06 #5
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
Nov 7 '06 #6
Anton81 wrote:
>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?
Yes. If you use your class with and without the __future__ option you have
to implement both __div__ and __truediv__, I suppose.

Peter
Nov 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: rss | last post by:
SUMMARY: ========== I am unable to pass along a simple Cookie obtained from a HttpWebRequest call (Machine A's ASP.NET app) so that another Web Server (Machine B) recongnizes the cookie. I...
4
by: Ê÷Éϲä»Ò | last post by:
'rar' is not recognized as an internal or external command, operable program or batch file. import os import time source = target_dir = r'e:\temp\bak' target =...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
3
by: Bob | last post by:
I need to create a program that is essentially a special fax sender using multi line Dialogic cards. I figure that the best way to do this so that it can be used from any app is to create someting...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
2
by: ruthiefy | last post by:
Hi, I need a few sql queries results so I created a new class with has a few functions that load the data. After executing ExecuteReader, I need to load the data to the dropdownlist control....
5
by: James | last post by:
When using 2005 I create a new Form and VS displays the .Designer file below the main .cs file at the same level as the resx file. But, if I manually create a MyFom.cs file and a...
0
by: =?Utf-8?B?bWFydGlqbmthYWc=?= | last post by:
Hi, In our webapplications we work with global resource files wich are referenced as: <asp:Literal ID="litWaiting" runat="server"...
0
by: fantasticamir | last post by:
I have a simple program using a couple of classes and it compiled successfully but while it is linking I have the following problem: g++ -Wall -lcu -lglu ACL2MDD.o Link.o Rule.o reachability.o ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.