ch06: update from book draft
This commit is contained in:
29
06-obj-ref/bus.py
Normal file
29
06-obj-ref/bus.py
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
"""
|
||||
>>> import copy
|
||||
>>> bus1 = Bus(['Alice', 'Bill', 'Claire', 'David'])
|
||||
>>> bus2 = copy.copy(bus1)
|
||||
>>> bus3 = copy.deepcopy(bus1)
|
||||
>>> bus1.drop('Bill')
|
||||
>>> bus2.passengers
|
||||
['Alice', 'Claire', 'David']
|
||||
>>> bus3.passengers
|
||||
['Alice', 'Bill', 'Claire', 'David']
|
||||
|
||||
"""
|
||||
|
||||
# tag::BUS_CLASS[]
|
||||
class Bus:
|
||||
|
||||
def __init__(self, passengers=None):
|
||||
if passengers is None:
|
||||
self.passengers = []
|
||||
else:
|
||||
self.passengers = list(passengers)
|
||||
|
||||
def pick(self, name):
|
||||
self.passengers.append(name)
|
||||
|
||||
def drop(self, name):
|
||||
self.passengers.remove(name)
|
||||
# end::BUS_CLASS[]
|
||||
Reference in New Issue
Block a user