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

Image
Figure 8-1.  

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
Figure 8-2.  

Cubicle conversation

Image
Figure 8-3.  

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 ...

Get Head First Learn to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.