O'Reilly logo

Head First Learn to Code by Eric Freeman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 9. 7 modules, methods, classes, and objects: Getting Modular

Image

Your code is growing in size and complexity. As that happens you need better ways to abstract, to modularize, to organize your code. You’ve seen that functions can be used to group lines of code together into bundles you can reuse over and over. And you’ve also seen that collections of functions and variables can be placed into modules so that they can be more easily shared and reused. In this chaper we’ll revisit modules and learn how to use them even more effectively (so you’re all ready to share your code with others) and then we’re going to look at the ultimate in code reuse: objects. You’re going to see that Python objects are all around you, just waiting to be used.

Image

Cubicle conversation

Image

Greg: That’s easy; just ship him analyze.py. He can just import it and call the compute_readability function. Done!

Frank: Totally. Modules are just Python files; just ship it.

Joe: I don’t think that is quite right. I know we’ve said a file is just a module and all that, and we’ve certainly used import, but I think there is more to it than that.

Greg: Like what?

Joe: Well, for instance, right now, if you’ll remember, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required