ch06: update from book draft
This commit is contained in:
parent
1df34f2945
commit
d63b4844f1
4
06-obj-ref/README.rst
Normal file
4
06-obj-ref/README.rst
Normal file
@ -0,0 +1,4 @@
|
||||
Sample code for Chapter 8 - "Object references, mutability and recycling"
|
||||
|
||||
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
|
||||
http://shop.oreilly.com/product/0636920032519.do
|
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[]
|
28
06-obj-ref/cheese.py
Normal file
28
06-obj-ref/cheese.py
Normal file
@ -0,0 +1,28 @@
|
||||
"""
|
||||
>>> import weakref
|
||||
>>> stock = weakref.WeakValueDictionary()
|
||||
>>> catalog = [Cheese('Red Leicester'), Cheese('Tilsit'),
|
||||
... Cheese('Brie'), Cheese('Parmesan')]
|
||||
...
|
||||
>>> for cheese in catalog:
|
||||
... stock[cheese.kind] = cheese
|
||||
...
|
||||
>>> sorted(stock.keys())
|
||||
['Brie', 'Parmesan', 'Red Leicester', 'Tilsit']
|
||||
>>> del catalog
|
||||
>>> sorted(stock.keys())
|
||||
['Parmesan']
|
||||
>>> del cheese
|
||||
>>> sorted(stock.keys())
|
||||
[]
|
||||
"""
|
||||
|
||||
# tag::CHEESE_CLASS[]
|
||||
class Cheese:
|
||||
|
||||
def __init__(self, kind):
|
||||
self.kind = kind
|
||||
|
||||
def __repr__(self):
|
||||
return 'Cheese(%r)' % self.kind
|
||||
# end::CHEESE_CLASS[]
|
47
06-obj-ref/haunted_bus.py
Normal file
47
06-obj-ref/haunted_bus.py
Normal file
@ -0,0 +1,47 @@
|
||||
"""
|
||||
>>> bus1 = HauntedBus(['Alice', 'Bill'])
|
||||
>>> bus1.passengers
|
||||
['Alice', 'Bill']
|
||||
>>> bus1.pick('Charlie')
|
||||
>>> bus1.drop('Alice')
|
||||
>>> bus1.passengers
|
||||
['Bill', 'Charlie']
|
||||
>>> bus2 = HauntedBus()
|
||||
>>> bus2.pick('Carrie')
|
||||
>>> bus2.passengers
|
||||
['Carrie']
|
||||
>>> bus3 = HauntedBus()
|
||||
>>> bus3.passengers
|
||||
['Carrie']
|
||||
>>> bus3.pick('Dave')
|
||||
>>> bus2.passengers
|
||||
['Carrie', 'Dave']
|
||||
>>> bus2.passengers is bus3.passengers
|
||||
True
|
||||
>>> bus1.passengers
|
||||
['Bill', 'Charlie']
|
||||
|
||||
|
||||
>>> dir(HauntedBus.__init__) # doctest: +ELLIPSIS
|
||||
['__annotations__', '__call__', ..., '__defaults__', ...]
|
||||
>>> HauntedBus.__init__.__defaults__
|
||||
(['Carrie', 'Dave'],)
|
||||
>>> HauntedBus.__init__.__defaults__[0] is bus2.passengers
|
||||
True
|
||||
|
||||
"""
|
||||
|
||||
# tag::HAUNTED_BUS_CLASS[]
|
||||
class HauntedBus:
|
||||
"""A bus model haunted by ghost passengers"""
|
||||
|
||||
def __init__(self, passengers=[]): # <1>
|
||||
self.passengers = passengers # <2>
|
||||
|
||||
def pick(self, name):
|
||||
self.passengers.append(name) # <3>
|
||||
|
||||
def drop(self, name):
|
||||
self.passengers.remove(name)
|
||||
# end::HAUNTED_BUS_CLASS[]
|
||||
|
26
06-obj-ref/twilight_bus.py
Normal file
26
06-obj-ref/twilight_bus.py
Normal file
@ -0,0 +1,26 @@
|
||||
"""
|
||||
>>> basketball_team = ['Sue', 'Tina', 'Maya', 'Diana', 'Pat']
|
||||
>>> bus = TwilightBus(basketball_team)
|
||||
>>> bus.drop('Tina')
|
||||
>>> bus.drop('Pat')
|
||||
>>> basketball_team
|
||||
['Sue', 'Maya', 'Diana']
|
||||
"""
|
||||
|
||||
# tag::TWILIGHT_BUS_CLASS[]
|
||||
class TwilightBus:
|
||||
"""A bus model that makes passengers vanish"""
|
||||
|
||||
def __init__(self, passengers=None):
|
||||
if passengers is None:
|
||||
self.passengers = [] # <1>
|
||||
else:
|
||||
self.passengers = passengers #<2>
|
||||
|
||||
def pick(self, name):
|
||||
self.passengers.append(name)
|
||||
|
||||
def drop(self, name):
|
||||
self.passengers.remove(name) # <3>
|
||||
# end::TWILIGHT_BUS_CLASS[]
|
||||
|
Loading…
Reference in New Issue
Block a user