Section | Video Links |
---|---|
Builder Overview | |
Builder Use Case | |
Python List |
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
python ./builder/builder_concept.py
['a', 'b', 'c']
... Refer to Book or Design Patterns In Python website to read textual content.
python ./builder/client.py
This is a Ice Igloo with 1 door(s) and 0 window(s).
This is a Sandstone Castle with 100 door(s) and 200 window(s).
This is a Wood House Boat with 6 door(s) and 8 window(s).
In the file /builder/builder_concept.py
def __init__(self):
self.parts = []
The []
is indicating a Python List.
The list can store multiple items, they can be changed, they can have items added and removed, can be re-ordered, can be pre-filled with items when instantiated and is also very flexible.
PS> python
>>> items = []
>>> items.append("shouldn't've")
>>> items.append("y'aint")
>>> items.extend(["whomst", "superfluity"])
>>> items
["shouldn't've", "y'aint", 'whomst', 'superfluity']
>>> items.reverse()
>>> items
['superfluity', 'whomst', "y'aint", "shouldn't've"]
>>> items.remove("y'aint")
>>> items
['superfluity', 'whomst', "shouldn't've"]
>>> items.insert(1, "phoque")
>>> items
['superfluity', 'phoque', 'whomst', "shouldn't've"]
>>> items.append("whomst")
>>> items.count("whomst")
2
>>> len(items)
5
>>> items[2] = "bagnose"
>>> items
['superfluity', 'phoque', 'bagnose', "shouldn't've", 'whomst']
>>> items[-2]
"shouldn't've"
Lists are used in almost every code example in this book. You will see all the many ways they can be used.
In fact, a list was used in the /abstract_factory/furniture_factory.py example,
if furniture in ['SmallChair', 'MediumChair', 'BigChair']:
...
This line, creates a list at runtime including the strings 'SmallChair', 'MediumChair' and 'BigChair'. If the value in furniture
equals the same string as one of those items in the list, then the condition is true and the code within the if statement block will execute.
... Refer to Book or Design Patterns In Python website to read textual content.