unsupported operand type(s)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tanusreesen
    New Member
    • Dec 2006
    • 1

    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
    Code:
    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__.poi nt 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'
    Last edited by bartonc; Dec 7 '06, 07:01 PM. Reason: added code tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by tanusreesen
    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
    Code:
    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__.poi nt 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!

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Also __str__!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by tanusreesen
        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
        Code:
        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__.poi nt 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.
        Code:
        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
        Code:
        (2.5000, 3.5000)
        (5.0000, 7.0000)
        (2.5000, 3.5000)
        (30.0000, 42.0000)

        Comment

        Working...