In [1]:
class Pair:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Pair({0.x!r}, {0.y!r})'.format(self)
def __str__(self):
return '({0.x!s}, {0.y!s})'.format(self)
In [3]:
p = Pair(3,4)
In [4]:
print('p is {0}'.format(p))
In [5]:
class PairX:
def __init__(self, x, y):
self.x = x
self.y = y
In [6]:
pX = PairX(3,4)
In [7]:
print('p is {0}'.format(pX))
In [8]:
_formats = {
'ymd' : '{d.year}-{d.month}-{d.day}',
'mdy' : '{d.month}/{d.day}/{d.year}',
'dmy' : '{d.day}/{d.month}/{d.year}'
}
class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
def __format__(self, code):
if code == '':
code = 'ymd'
fmt = _formats[code]
return fmt.format(d=self)
In [9]:
d = Date(2012, 12, 21)
In [10]:
format(d)
Out[10]:
In [1]:
#Create a linked list.
class Node:
def __init__(self, cargo=None, next=None):
self.cargo = cargo
self.next = next
def __str__(self):
return str(self.cargo)
In [2]:
n1 = Node(1)
n2 = Node(2)
n3 = Node(3)
In [4]:
n1.next = n2
n2.next = n3
In [6]:
print(n1.next)
In [9]:
def printList(node):
while node:
print (node)
node = node.next
In [11]:
printList(n1)
In [18]:
def removeSecond(list):
if list == None: return
first = list
second = list.next
# make the first node refer to the third
first.next = second.next
# separate the second node from the rest of the list
second.next = None
return second
In [19]:
removed = removeSecond(n1)
In [21]:
print(removed)
In [24]:
printList(n1)
In [ ]: