473,395 Members | 1,488 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,395 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 11797
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 I've looked for hours for some type of error, and...
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 : running build_ext Traceback (most recent call...
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 tuple. When I execute the following python...
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 bs('tr', {'bgcolor' : '#eeeeee'} | {'bgcolor' :...
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 gradiant functions. cStepCurveEvaluator.cpp...
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>", line 1, in ? TypeError: unsupported operand type(s)...
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 more complicated when unsigned operands 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 occurs from time to time. Can anyone give me some...
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): a = n b = n while a>0 and b>1: n = (n)*(b-1)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.