By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

unsupported operand type(s)

P: 1
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
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
Also __str__!
Dec 7 '06 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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

Post your reply

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