472,348 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

unsupported operand type(s)

Hi All,
I am completely new to python programming language.I was trying to write a program on class.But I am getting this error.Can someone please help me to solve the error I am facing.I am using python2.5.
This is exactly what I wrote and still came up with this error
Expand|Select|Wrap|Line Numbers
  1. class point:
  2.     def __init__(self,x=0,y=0):
  3.         self.x=x
  4.         self.y=y
  5.     def _str_(self):
  6.         return '('+ str(self.x)+',  ' + str(self.y) + ')'
  7.     def _add_(self,other):
  8.         return point(self.x+ other.x,self.y+other.y)
>>> p1 =point(3,4)
>>> p2 = point(5,3)
>>> print p1._add_(p2)
<__main__.point instance at 0x0117ED28>
>>> p3 = p1+p2

Traceback (most recent call last):
File "<pyshell#301>", line 1, in <module>
p3 = p1+p2
TypeError: unsupported operand type(s) for +: 'instance' and 'instance'
Dec 7 '06 #1
3 11507
bartonc
6,596 Expert 4TB
Hi All,
I am completely new to python programming language.I was trying to write a program on class.But I am getting this error.Can someone please help me to solve the error I am facing.I am using python2.5.
This is exactly what I wrote and still came up with this error
Expand|Select|Wrap|Line Numbers
  1. class point:
  2.     def __init__(self,x=0,y=0):
  3.         self.x=x
  4.         self.y=y
  5.     def _str_(self):
  6.         return '('+ str(self.x)+',  ' + str(self.y) + ')'
  7.     def _add_(self,other):
  8.         return point(self.x+ other.x,self.y+other.y)
>>> p1 =point(3,4)
>>> p2 = point(5,3)
>>> print p1._add_(p2)
<__main__.point instance at 0x0117ED28>
>>> p3 = p1+p2

Traceback (most recent call last):
File "<pyshell#301>", line 1, in <module>
p3 = p1+p2
TypeError: unsupported operand type(s) for +: 'instance' and 'instance'
Special functions (like __init__ and __add__) need two underscores pre and post!
Dec 7 '06 #2
bartonc
6,596 Expert 4TB
Also __str__!
Dec 7 '06 #3
bvdet
2,851 Expert Mod 2GB
Hi All,
I am completely new to python programming language.I was trying to write a program on class.But I am getting this error.Can someone please help me to solve the error I am facing.I am using python2.5.
This is exactly what I wrote and still came up with this error
Expand|Select|Wrap|Line Numbers
  1. class point:
  2.     def __init__(self,x=0,y=0):
  3.         self.x=x
  4.         self.y=y
  5.     def _str_(self):
  6.         return '('+ str(self.x)+',  ' + str(self.y) + ')'
  7.     def _add_(self,other):
  8.         return point(self.x+ other.x,self.y+other.y)
>>> p1 =point(3,4)
>>> p2 = point(5,3)
>>> print p1._add_(p2)
<__main__.point instance at 0x0117ED28>
>>> p3 = p1+p2

Traceback (most recent call last):
File "<pyshell#301>", line 1, in <module>
p3 = p1+p2
TypeError: unsupported operand type(s) for +: 'instance' and 'instance'
Neat post tanusreesen! You are close.
Expand|Select|Wrap|Line Numbers
  1. class Point(object):
  2.     def __init__(self, x=0.0, y=0.0):
  3.         self.x = x
  4.         self.y = y
  5.     def __str__(self):
  6.         return '(%0.4f, %0.4f)' % (self.x, self.y)
  7.     def __add__(self, other):
  8.         return Point(self.x + other.x, self.y + other.y)
  9.     def __sub__(self, other):
  10.         return Point(self.x - other.x, self.y - other.y)
  11.     def __mul__(self, factor):
  12.         return Point(self.x * factor, self.y * factor)
  13.  
  14.  
  15. p1 = Point(2.5, 3.5)
  16. print p1
  17. p2 =  p1 + (Point(2.5, 3.5))
  18. print p2
  19. p3 = p2 - p1
  20. print p3
  21. p4 = p2 * 6
  22. print p4
Output
Expand|Select|Wrap|Line Numbers
  1. (2.5000, 3.5000)
  2. (5.0000, 7.0000)
  3. (2.5000, 3.5000)
  4. (30.0000, 42.0000)
Dec 7 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: DJ Craig | last post by:
I keep getting this error: Fatal error: Unsupported operand types in /usr/local/etc/httpd/htdocs/djtricities/kat/bpmchart/chart.php on line 58 ...
3
by: Martin Koekenberg | last post by:
Hello, Ik get the following error when I install a new Python software package such as Zope or PIL. This is what I get whem I 'make' Zope : ...
3
by: Rakesh | last post by:
In my Python code fragment, I want to write a code fragment such that the minimum element of a tuple is subtracted from all the elements of a given...
3
by: yaffa | last post by:
hey folks i get this error: Python interpreter error: unsupported operand type(s) for |: when i run this line of code: for incident in...
6
by: BlueTrin | last post by:
Hello I was adapting a C version of SolvOpt in C++ to use it within a virtual class. However I am stuck with the overriding of evaluation and...
1
by: Florian Lindner | last post by:
Hello, I get the exception above when trying to use a float as radix and exponent, e.g.: Traceback (most recent call last): File "<stdin>",...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are...
8
by: Gilbert Fine | last post by:
This is a very strange exception raised from somewhere in our program. I have no idea how this happen. And don't know how to reproduce. It just...
2
by: Jordan Harry | last post by:
I'm trying to write a simple program to calculate permutations. I created a file called "mod.py" and put the following in it: def factorial(n):...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.