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 -
class point:
-
def __init__(self,x=0,y=0):
-
self.x=x
-
self.y=y
-
def _str_(self):
-
return '('+ str(self.x)+', ' + str(self.y) + ')'
-
def _add_(self,other):
-
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'
3 11507
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 -
class point:
-
def __init__(self,x=0,y=0):
-
self.x=x
-
self.y=y
-
def _str_(self):
-
return '('+ str(self.x)+', ' + str(self.y) + ')'
-
def _add_(self,other):
-
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!
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 -
class point:
-
def __init__(self,x=0,y=0):
-
self.x=x
-
self.y=y
-
def _str_(self):
-
return '('+ str(self.x)+', ' + str(self.y) + ')'
-
def _add_(self,other):
-
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. -
class Point(object):
-
def __init__(self, x=0.0, y=0.0):
-
self.x = x
-
self.y = y
-
def __str__(self):
-
return '(%0.4f, %0.4f)' % (self.x, self.y)
-
def __add__(self, other):
-
return Point(self.x + other.x, self.y + other.y)
-
def __sub__(self, other):
-
return Point(self.x - other.x, self.y - other.y)
-
def __mul__(self, factor):
-
return Point(self.x * factor, self.y * factor)
-
-
-
p1 = Point(2.5, 3.5)
-
print p1
-
p2 = p1 + (Point(2.5, 3.5))
-
print p2
-
p3 = p2 - p1
-
print p3
-
p4 = p2 * 6
-
print p4
Output - (2.5000, 3.5000)
-
(5.0000, 7.0000)
-
(2.5000, 3.5000)
-
(30.0000, 42.0000)
Sign in to post your reply or Sign up for a free account.
Similar topics
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
...
|
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 :
...
|
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...
|
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...
|
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...
|
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>",...
|
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...
|
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...
|
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):...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
| |