Custom Search

Saturday, January 22, 2011

python defining special method __add__ on a class

python defining special method __add__ on a class

======================== 1


class A:

def __init__(self, num):
self.num = num

def __add__(self, second_num):
return self.num + second_num


a1 = A(10)

print "-------a1 + 10-------", a1 + 40
print "-------a1 + a1-------", a1 + a1


OUTPUT
=======
-------a1 + 10------- 50 <--------
-------a1 + a1-------
Traceback (most recent call last):
File "sp.py", line 16, in
print "-------a1 + a1-------", a1 + a1
File "sp.py", line 10, in __add__
return self.num + second_num
TypeError: unsupported operand type(s) for +: 'int' and 'instance'


======================== 2

class A:

def __init__(self, num):
self.num = num

def __add__(self, second_object):
return self.num + second_object.num


a1 = A(10)

print "-------a1 + a1-------", a1 + a1
print "-------a1 + 10-------", a1 + 40


OUTPUT
=======
-------a1 + a1------- 20 <--------
-------a1 + 10-------
Traceback (most recent call last):
File "sp.py", line 16, in
print "-------a1 + 10-------", a1 + 40
File "sp.py", line 10, in __add__
return self.num + second_object.num
AttributeError: 'int' object has no attribute 'num'



======================== 3

class a:
def __init__(self):
pass

a1 = a()

print "-------a1 + a1-------", a1 + a1


OUTPUT
========
test@saju-laptop:~/Desktop$ python sp.py
-------a1 + a1-------
Traceback (most recent call last):
File "sp.py", line 10, in
print "-------a1 + a1-------", a1 + a1
TypeError: unsupported operand type(s) for +: 'instance' and 'instance'


======================== 4

class A:
def __init__(self):
pass

def __add__(self, second_item):
print "\n-------in------__add__-------"
print "\n-------first item-------", self
print "\n-------second item------", second_item
#Note: Here items first and second are same(same object 'a1' of class 'A')

a1 = A()

print "--------a1---------", a1

print "\n\***********\n"

print "-------a1 + a1-------", a1 + a1

print "\n\***********\n"

print "---------a1.__add__(a1)------------", a1.__add__(a1)

#Note: Here "a1 + a1" and "a1.__add__(a1)" are same.




OUTPUT
=======
--------a1--------- <__main__.a>

\***********

-------a1 + a1-------
-------in------__add__-------

-------first item------- <__main__.a>

-------second item------ <__main__.a>
None

\***********

---------a1.__add__(a1)------------
-------in------__add__-------

-------first item------- <__main__.a>

-------second item------ <__main__.a>
None


======================== 5

class A:

def __init__(self):
pass

def __add__(self, second_item):
print "\n-------in------__add__-------"
print "\n-------first item-------", self
print "\n-------second item------", second_item
#Note: Here items first 'a1' and second 'a2' are different

a1 = A()
a2 = A()

print "--------a1---------", a1
print "--------a2---------", a2

print "\n\***********\n"

print "-------a1 + a2-------", a1 + a2

print "\n\***********\n"

print "---------a1.__add__(a2)------------", a1.__add__(a2)

#Here "a1 + a2" and "a1.__add__(a2)" are same.


OUTPUT
=======
--------a1--------- <__main__.a>
--------a2--------- <__main__.a>

\***********

-------a1 + a2-------
-------in------__add__-------

-------first item------- <__main__.a>

-------second item------ <__main__.a>
None

\***********

---------a1.__add__(a2)------------
-------in------__add__-------

-------first item------- <__main__.a>

-------second item------ <__main__.a>
None


========================

No comments:

Post a Comment