Feb
25

# Python Dictionary Coolness

Lately I’ve been working with dictionaries, specifically as a device for storing settings. I ran into a little annoyance and thought of programming a little routine to take care of it. Come to find out, Python already has such a routine built into the dictionary class. Read on to find out more.


*args and **kwargs

Let’s suppose I have the following dictionary of settings:

settings = {
    'left': 100,
    'top': 100,
    'height': 500,
    'width': 500,
}

I also have a function for changing settings. Let’s call it change_settings just to be obvious.

Python functions can handle extra arguments and keyword arguments with just a little function definition trick. For instance, trying to call change_settings(1, 2, 3) when the function definition is def change_settings(number): would be problematic. The function only takes one argument and three were given. If, however, you define the function as def change_settings(number, *args):, the 2 and 3 will be stored as the tuple (2, 3) in the variable args. (args is just the typical name for this variable. In reality it can be anything, though it is not recommended for reasons of transparency.)

The same things can be done with keyword arguments, only instead of using one asterisk, use two. The keyword arguments are stored in a dictionary, where the keyword is a string (one of the keys) and the value is the value for that key. The dictionary is generally called kwargs for ‘keyword arguments’.

The function definition now looks like this:

def change_settings(number, *args, **kwargs):

If we make a call such as change_settings(1, 2, 3, 4, color='white', size=400), the value of number inside the function is 1, the value of args is (2, 3, 4), and the value of kwargs is the dictionary {‘color’: ‘white’, ‘size’: 400}.

Changing Settings

Now, if I want to change my default settings, all I have to do is make declare a function as def change_settings(**kwargs):. Then I can call change_settings(left=200, top=30, width=400, height=60). This is passed into the change_settings function as the dictionary

kwargs = {
    'left': 200,
    'top': 30,
    'width': 400,
    'height': 60,
}

Then, to update the settings, all I have to do is set settings equal to kwargs. This, however, will replace the original settings dictionary with the new keyword arguments dictionary, which means that every time I call change_settings, I have to set every single value, and if I don’t want some of them to change, I have to set them to be what they were before.

What I would like to do is change only certain values in settings using the keyword arguments dictionary. In other words, if I want to change only the ‘width’ setting, I want to be able to call change_settings(width=80) without losing all the other settings as well.

One way to do this is to run through all the keys in kwargs and replace the values of settings for those keys with the values in kwargs. The code for this is pretty simple.

for key in kwargs.keys():
    settings[key] = kwargs[key]

This is fairly straight forward, but I wanted it to be simpler. A little poking around gave me the solution. Dictionaries have a built-in method which already does this: update. Using the update method, the code above can be accomplished with one line:

settings.update(kwargs)

This does the exact same thing as the for loop.

Conclusion

This is why I use Python for my everyday programming. It’s not only fast and easy, but often there are built-in ways to simplify common tasks.

  • Digg
  • del.icio.us
  • Facebook
  • Google Buzz
  • Google Reader
  • Reddit
  • NewsVine
  • RSS
  • Twitter
  • Technorati
  • StumbleUpon

3 Comments to “# Python Dictionary Coolness”

  • Enjoyed the blog post!

  • Hello from Russia!
    Can I quote a post in your blog with the link to you?

  • Quote away! I’m glad you enjoyed it. – Eric

Leave a comment