Tuesday, November 11, 2008

Chapter 7

Alright, to be perfectly honest, I seem to be having a lot of difficulty with some of these programs that are in the book. I think I might be using the wrong GUI. I’ll bring my computer in to our meeting tomorrow so we can sit down and look at it. I also have a couple questions about chapter 5 and 6 that are bothering me.

In chapter 7, we are introduced to modules. Modules create a named scope for functions and data, but they are simpler than classes and objects. They let you separate your program into named pieces without having to use classes. Much of Pythons flexibility comes from its ability to use modules to expand its features. For example, there could be a networking module or an operating system module, etc.

In order to use a module, you have to first install it in the system. The easiest way to do this is to use the import command:

Import sys

This line will import the module named sys which contains services Python offers that mostly involve system-specific items.


To create a module, all you have to do is create a file with what you want to call it and the extension “.py”. In the book they use “Foods.py”. When we’re finished, we can import it using the name Foods without the extension. Below is a line of code that imports the Food module:

>>>import Foods
>>>dir(Foods)
[‘Fridge’, ‘Omelet’, ‘Recipe’, ‘_builtins_’, ‘_doc_’, ‘_file_’, ‘_name_’ ]
>>>

This lets us use the Fridge, Omelet, and Recipe classes. In order to access them now, we have to use Foods.Fridge, Foods.Omelet, and Foods.Recipe.

No comments: