In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [1]:
class Point():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return '({}, {})'.format(self.x, self.y)
def __add__(self, other):
if isinstance(other, Point):
return self.add_point(other)
elif isinstance(other, tuple):
return self.add_tuple(other)
def add_point(self, other):
return Point(self.x + other.x, self.y + other.y)
def add_tuple(self, in_tuple):
return Point(self.x + in_tuple[0], self.y + in_tuple[1])
def __radd__(self, other):
return self.__add__(other)
In [2]:
p1 = Point(23, 46)
p2 = Point(1, 2)
print(p1 + p2)
In [3]:
p1 = Point(23, 46)
tup = (10, 20)
print(p1 + tup)
In [4]:
p1 = Point(23, 46)
tup = (10, 20)
print(tup + p1)
In [ ]:
In [23]:
# Directly copied from the solutions provided in the book!!!!!
# Ver 1, with poential BUG!
class Kangaroo(object):
"""a Kangaroo is a marsupial"""
def __init__(self, contents=[]):
"""initialize the pouch contents; the default value is
an empty list"""
self.pouch_contents = contents
def __str__(self):
"""return a string representaion of this Kangaroo and
the contents of the pouch, with one item per line"""
t = [ object.__str__(self) + ' with pouch contents:' ]
for obj in self.pouch_contents:
s = ' ' + object.__str__(obj)
t.append(s)
return '\n'.join(t)
def put_in_pouch(self, item):
"""add a new item to the pouch contents"""
self.pouch_contents.append(item)
kanga = Kangaroo()
roo = Kangaroo()
kanga.put_in_pouch('wallet')
kanga.put_in_pouch('car keys')
kanga.put_in_pouch(roo)
print kanga
print roo
In [15]:
# Ver 2
class Kangaroo(object):
"""a Kangaroo is a marsupial"""
def __init__(self, contents=[]):
# The problem is the default value for contents.
# Default values get evaluated ONCE, when the function
# is defined; they don't get evaluated again when the
# function is called.
# In this case that means that when __init__ is defined,
# [] gets evaluated and contents gets a reference to
# an empty list.
# After that, every Kangaroo that gets the default
# value get a reference to THE SAME list. If any
# Kangaroo modifies this shared list, they all see
# the change.
# The next version of __init__ shows an idiomatic way
# to avoid this problem.
self.pouch_contents = contents
def __init__(self, contents=None):
# In this version, the default value is None. When
# __init__ runs, it checks the value of contents and,
# if necessary, creates a new empty list. That way,
# every Kangaroo that gets the default value get a
# reference to a different list.
# As a general rule, you should avoid using a mutable
# object as a default value, unless you really know
# what you are doing.
if contents == None:
contents = []
self.pouch_contents = contents
def __str__(self):
"""return a string representation of this Kangaroo and
the contents of the pouch, with one item per line"""
t = [ object.__str__(self) + ' with pouch contents:' ]
for obj in self.pouch_contents:
s = ' ' + object.__str__(obj)
t.append(s)
return '\n'.join(t)
def put_in_pouch(self, item):
"""add a new item to the pouch contents"""
self.pouch_contents.append(item)
kanga = Kangaroo()
roo = Kangaroo()
kanga.put_in_pouch('wallet')
kanga.put_in_pouch('car keys')
kanga.put_in_pouch(roo)
print kanga
print ''
print roo
In [ ]: