Section | Video Links |
---|---|
Observer Overview | |
Observer Use Case | |
Python Set |
... 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 ./observer/observer_concept.py
Observer id:2084220160272 received ('First Notification', [1, 2, 3])
Observer id:2084220160224 received ('First Notification', [1, 2, 3])
Observer id:2084220160272 received ('Second Notification', {'A': 1, 'B': 2, 'C': 3})
... Refer to Book or Design Patterns In Python website to read textual content.
python ./observer/client.py
PieGraph, id:1
Drawing a Pie graph using data:[1, 2, 3]
BarGraph, id:2
Drawing a Bar graph using data:[1, 2, 3]
TableView, id:3
Drawing a Table view using data:[1, 2, 3]
PieGraph, id:1
Drawing a Pie graph using data:[4, 5, 6]
TableView, id:3
Drawing a Table view using data:[4, 5, 6]
A Python Set is similar to a List. Except that the items in the Set are guaranteed to be unique, even if you try to add a duplicate. A set is a good choice for keeping a collection of observables, since the problem of duplicate observables is automatically handled.
A Set can be instantiated using the curly braces {}
or set()
, verses []
for a List and ()
for a Tuple. It is not the same as a Dictionary, which also uses {}
, since the dictionary items are created as key:value
pairs. ie {"a": 1, "b": 2, "c": 3}
PS> python
>>> items = {"yankee", "doodle", "dandy", "doodle"}
>>> items
{'yankee', 'doodle', 'dandy'}
>>> items.add("grandy")
>>> items
{'grandy', 'yankee', 'doodle', 'dandy'}
>>> items.remove("doodle")
>>> items
{'grandy', 'yankee', 'dandy'}
Note, if instantiating an empty Set then use my_object = Set()
rather than my_object = {}
to reduce ambiguity with creating an empty Dictionary.
... Refer to Book or Design Patterns In Python website to read textual content.