Section | Video Links |
---|---|
Singleton Overview | |
Singleton Use Case | |
Python Dictionary |
... Refer to Book or Design Patterns In Python website to read textual content.
python ./singleton/singleton_concept.py
id(Singleton) = 2164775087968
id(OBJECT1) = 2164775087968
id(OBJECT2) = 2164775087968
id(OBJECT3) = 2164775087968
... 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 ./singleton/client.py
-----------Leaderboard-----------
| 1 | Emmy |
| 2 | Cosmo |
| 3 | Sean |
-----------Leaderboard-----------
| 1 | Emmy |
| 2 | Cosmo |
| 3 | Sean |
-----------Leaderboard-----------
| 1 | Emmy |
| 2 | Cosmo |
| 3 | Sean |
In the file /singleton/leaderboard.py,
"The Leaderboard as a Singleton"
_table = {}
The {}
is indicating a Python Dictionary.
A Dictionary can be instantiated using the curly braces {}
or dict()
The Dictionary is similar to a List, except that the items are key:value
pairs.
The Dictionary can store multiple key:value
pairs, they can be changed, can be added and removed, can be re-ordered, can be pre-filled with key:value
pairs when instantiated and is very flexible.
Since Python 3.7, dictionaries are ordered in the same way that they are created.
The keys of the dictionary are unique.
You can refer to the dictionary items by key, which will return the value.
PS> python
>>> items = {"abc": 123, "def": 456, "ghi": 789}
>>> items["abc"]
123
You can change the value at a key,
PS> python
>>> items = {"abc": 123, "def": 456, "ghi": 789}
>>> items["def"] = 101112
>>> items["def"]
101112
You can add new key:value
pairs, and remove them by using the key.
PS> python
>>> items = {"abc": 123, "def": 456, "ghi": 789}
>>> items["jkl"] = 101112
>>> items["jkl"]
101112
>>> items.pop('def')
456
>>> items
{'abc': 123, 'ghi': 789, 'jkl': 101112}
You can order a dictionary alphabetically by key
PS> python
>>> items = {"abc": 123, "ghi": 789, "def": 456}
>>> items
{'abc': 123, 'ghi': 789, 'def': 456}
>>> dict(sorted(items.items()))
{'abc': 123, 'def': 456, 'ghi': 789}
... Refer to Book or Design Patterns In Python website to read textual content.