It is clear that the actual addition of associative arrays (they are also hashes, dicts, etc. in different languages) does not exist, since It is not at all obvious what it should give. But sometimes it is convenient to have a simple, easy-to-read operation that easily and simply combines hashes. This is especially useful when there are a number of hashes, and you need to take several keys with different priorities from there. If there is no value in the first, then we check the second, if not, and there, then the third, etc.
You have 4 hashes of global_config, local_config, db_row, user_info
Just below the habrakat, I will show you how creepy the construction of 4 lines
values = global_config.copy ()
values.update (local_config)
values.update (db_row)
values.update (user_info)
Write one
values = global_config + local_config + db_row + user_info
It is clear that "in the forehead" it will not work. Hashes do not know such an operation. Therefore, we will create our own class based on the base dict, which can be added:
class AddableDict (dict):
def __add __ (self, x):
s = AddableDict (self)
s.update (x)
return s
* This source code was highlighted with Source Code Highlighter .
')
Notice how easy this is done - we just need to add a method that will be called by the "+" operator.
The class behaves like a hash in everything: it also creates, adds key values, etc .:
>>> q = AddableDict (a = 2, b = 5)
>>> q
{ 'a' : 2, 'b' : 5}
>>> w = AddableDict ({ 'a' : 3})
>>> w
{ 'a' : 3}
>>> q [ 'a' ] = 444
>>> q
{ 'a' : 444, 'b' : 5}
>>> q [ 'g' ] = 'sdf'
>>> q
{ 'a' : 444, 'b' : 5, 'g' : 'sdf' }
>>>
* This source code was highlighted with Source Code Highlighter .
With the only difference that they can now be folded. If duplication of keys occurs, the values of the right replace the corresponding values of the left — all as in the .update () method.
>>> q + w
{ 'a' : 3, 'b' : 5}
* This source code was highlighted with Source Code Highlighter .
Where to get such hashes in the program? Do not rewrite it all over again! Moreover, there are a lot of standard features that don’t know about our new features.
Fortunately, the problem is solved easily. Any hash can be quickly turned into this:
>>> w = AddableDict ({ 'a' : 3})
>>> w
{ 'a' : 3}
* This source code was highlighted with Source Code Highlighter .
Where can I use?
Wherever you want to reduce the recording and improve the readability of the program.
In the current project, I often need to select information from several sources with different priorities, for example, the value from the database, the default value for this class, from the project settings, etc.
global_config = { 'a' : 1, 'b' : 4}
local_config = { 'a' : 7, 'c' : 1}
db_row = { 'a' : 10}
user_info = { 'c' : 0}
values = global_config.copy ()
values.update (local_config)
values.update (db_row)
values.update (user_info)
OR
values = global_config + local_config + db_row + user_info
* This source code was highlighted with Source Code Highlighter .
This article does not claim to be fundamental, it describes how to simplify writing for readability - “syntactic sugar”.
Thanks for the comments!