16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

116 CHAPTER 6 ■ ABSTRACTION<br />

s<strong>to</strong>rage = {}<br />

s<strong>to</strong>rage['first'] = {}<br />

s<strong>to</strong>rage['middle'] = {}<br />

s<strong>to</strong>rage['last'] = {}<br />

The data structure s<strong>to</strong>rage is a dictionary with three keys: 'first', 'middle', and 'last'.<br />

Under each of these keys, you s<strong>to</strong>re another dictionary. In these subdictionaries, you’ll use<br />

names (first, middle, or last) as keys, and insert lists of people as values. For example, <strong>to</strong> add me<br />

<strong>to</strong> this structure, you could do the following:<br />

>>> me = 'Magnus Lie Hetland'<br />

>>> s<strong>to</strong>rage['first']['Magnus'] = [me]<br />

>>> s<strong>to</strong>rage['middle']['Lie'] = [me]<br />

>>> s<strong>to</strong>rage['last']['Hetland'] = [me]<br />

Under each key, you s<strong>to</strong>re a list of people. In this case, the lists contain only me.<br />

Now, if you want a list of all the people registered who have the middle name Lie, you<br />

could do the following:<br />

>>> s<strong>to</strong>rage['middle']['Lie']<br />

['Magnus Lie Hetland']<br />

As you can see, adding people <strong>to</strong> this structure is a bit tedious, especially when you get<br />

more people with the same first, middle, or last names, because then you have <strong>to</strong> extend the list<br />

that is already s<strong>to</strong>red under that name. Let’s add my sister, for example, and let’s assume you<br />

don’t know what is already s<strong>to</strong>red in the database:<br />

>>> my_sister = 'Anne Lie Hetland'<br />

>>> s<strong>to</strong>rage['first'].setdefault('Anne', []).append(my_sister)<br />

>>> s<strong>to</strong>rage['middle'].setdefault('Lie', []).append(my_sister)<br />

>>> s<strong>to</strong>rage['last'].setdefault('Hetland', []).append(my_sister)<br />

>>> s<strong>to</strong>rage['first']['Anne']<br />

['Anne Lie Hetland']<br />

>>> s<strong>to</strong>rage['middle']['Lie']<br />

['Magnus Lie Hetland', 'Anne Lie Hetland']<br />

Imagine writing a large program filled with updates like this—it would quickly become<br />

quite unwieldy.<br />

The point of abstraction is <strong>to</strong> hide all the gory details of the updates, and you can do that<br />

with functions. Let’s first make a function <strong>to</strong> initialize a data structure:<br />

def init(data):<br />

data['first'] = {}<br />

data['middle'] = {}<br />

data['last'] = {}<br />

In the preceding code, I’ve simply moved the initialization statements inside a function.<br />

You can use it like this:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!