Head First Python

Page 71

sharing your code

Turn your function into a module A module is simply a text file that contains Python code. The main requirement is that the name of the file needs to end in .py: the Python extension. To turn your function into a module, save your code into an appropriately named file:

Your code from Chapter 1

def print_lol(the_list): for each_item in the_list:

if isinstance(each_item, list):

print_lol(each_item)

else:

print(each_item)

Let’s call this file “nester.py”.

Q: A:

What’s the best Python editor?

The answer to that question really depends on who you ask. However, you can, of course, use any text editor to create and save your function’s code in a text file. Something as simple as NotePad on Windows works fine for this, as does a full-featured editor such as TextMate on Mac OS X. And there’s also full-fledged IDEs such as Eclipse on Linux, as well as the classic vi and emacs editors. And, as you already know, Python comes with IDLE, which also includes a built-in code editor. It might not be as capable as those other “real” editors, but IDLE is installed with Python and is essentially guaranteed to be available. For lots of jobs, IDLE’s edit window is all the editor you’ll ever need when working with your Python code. Of course, there are other IDEs for Python, too. Check out WingIDE for one that specifically targets Python developers.

Do this!

Go ahead and create a text file called nester.py that contains your function code from the end of Chapter 1.

you are here 4   35


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.