Cover by Paul Barry

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 6. Custom data Objects: Bundling code with data

image with no caption

It’s important to match your data structure choice to your data. And that choice can make a big difference to the complexity of your code. In Python, although really useful, lists and sets aren’t the only game in town. The Python dictionary lets you organize your data for speedy lookup by associating your data with names, not numbers. And when Python’s built-in data structures don’t quite cut it, the Python class statement lets you define your own. This chapter shows you how.

Coach Kelly is back (with a new file format)

image with no caption

The output from your last program in Chapter 5 was exactly what the coach was looking for, but for the fact that no one can tell which athlete belongs to which data. Coach Kelly thinks he has the solution: he’s added identification data to each of his data files:

image with no caption

If you use the split() BIF to extract Sarah’s data into a list, the first data item is Sarah’s name, the second is her date of birth, and the rest is Sarah’s timing data.

Let’s exploit this format and see how well things work.

Do this!

Grab the updated files from the Head First Python website.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required