Skip to content

Commit d63b484

Browse files
committed
ch06: update from book draft
1 parent 1df34f2 commit d63b484

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

06-obj-ref/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Sample code for Chapter 8 - "Object references, mutability and recycling"
2+
3+
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
4+
http://shop.oreilly.com/product/0636920032519.do

06-obj-ref/bus.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
"""
3+
>>> import copy
4+
>>> bus1 = Bus(['Alice', 'Bill', 'Claire', 'David'])
5+
>>> bus2 = copy.copy(bus1)
6+
>>> bus3 = copy.deepcopy(bus1)
7+
>>> bus1.drop('Bill')
8+
>>> bus2.passengers
9+
['Alice', 'Claire', 'David']
10+
>>> bus3.passengers
11+
['Alice', 'Bill', 'Claire', 'David']
12+
13+
"""
14+
15+
# tag::BUS_CLASS[]
16+
class Bus:
17+
18+
def __init__(self, passengers=None):
19+
if passengers is None:
20+
self.passengers = []
21+
else:
22+
self.passengers = list(passengers)
23+
24+
def pick(self, name):
25+
self.passengers.append(name)
26+
27+
def drop(self, name):
28+
self.passengers.remove(name)
29+
# end::BUS_CLASS[]

06-obj-ref/cheese.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
>>> import weakref
3+
>>> stock = weakref.WeakValueDictionary()
4+
>>> catalog = [Cheese('Red Leicester'), Cheese('Tilsit'),
5+
... Cheese('Brie'), Cheese('Parmesan')]
6+
...
7+
>>> for cheese in catalog:
8+
... stock[cheese.kind] = cheese
9+
...
10+
>>> sorted(stock.keys())
11+
['Brie', 'Parmesan', 'Red Leicester', 'Tilsit']
12+
>>> del catalog
13+
>>> sorted(stock.keys())
14+
['Parmesan']
15+
>>> del cheese
16+
>>> sorted(stock.keys())
17+
[]
18+
"""
19+
20+
# tag::CHEESE_CLASS[]
21+
class Cheese:
22+
23+
def __init__(self, kind):
24+
self.kind = kind
25+
26+
def __repr__(self):
27+
return 'Cheese(%r)' % self.kind
28+
# end::CHEESE_CLASS[]

06-obj-ref/haunted_bus.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
>>> bus1 = HauntedBus(['Alice', 'Bill'])
3+
>>> bus1.passengers
4+
['Alice', 'Bill']
5+
>>> bus1.pick('Charlie')
6+
>>> bus1.drop('Alice')
7+
>>> bus1.passengers
8+
['Bill', 'Charlie']
9+
>>> bus2 = HauntedBus()
10+
>>> bus2.pick('Carrie')
11+
>>> bus2.passengers
12+
['Carrie']
13+
>>> bus3 = HauntedBus()
14+
>>> bus3.passengers
15+
['Carrie']
16+
>>> bus3.pick('Dave')
17+
>>> bus2.passengers
18+
['Carrie', 'Dave']
19+
>>> bus2.passengers is bus3.passengers
20+
True
21+
>>> bus1.passengers
22+
['Bill', 'Charlie']
23+
24+
25+
>>> dir(HauntedBus.__init__) # doctest: +ELLIPSIS
26+
['__annotations__', '__call__', ..., '__defaults__', ...]
27+
>>> HauntedBus.__init__.__defaults__
28+
(['Carrie', 'Dave'],)
29+
>>> HauntedBus.__init__.__defaults__[0] is bus2.passengers
30+
True
31+
32+
"""
33+
34+
# tag::HAUNTED_BUS_CLASS[]
35+
class HauntedBus:
36+
"""A bus model haunted by ghost passengers"""
37+
38+
def __init__(self, passengers=[]): # <1>
39+
self.passengers = passengers # <2>
40+
41+
def pick(self, name):
42+
self.passengers.append(name) # <3>
43+
44+
def drop(self, name):
45+
self.passengers.remove(name)
46+
# end::HAUNTED_BUS_CLASS[]
47+

06-obj-ref/twilight_bus.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
>>> basketball_team = ['Sue', 'Tina', 'Maya', 'Diana', 'Pat']
3+
>>> bus = TwilightBus(basketball_team)
4+
>>> bus.drop('Tina')
5+
>>> bus.drop('Pat')
6+
>>> basketball_team
7+
['Sue', 'Maya', 'Diana']
8+
"""
9+
10+
# tag::TWILIGHT_BUS_CLASS[]
11+
class TwilightBus:
12+
"""A bus model that makes passengers vanish"""
13+
14+
def __init__(self, passengers=None):
15+
if passengers is None:
16+
self.passengers = [] # <1>
17+
else:
18+
self.passengers = passengers #<2>
19+
20+
def pick(self, name):
21+
self.passengers.append(name)
22+
23+
def drop(self, name):
24+
self.passengers.remove(name) # <3>
25+
# end::TWILIGHT_BUS_CLASS[]
26+

0 commit comments

Comments
 (0)