The Main Block

Python may not have a main method, but; it does have something similar, the main block, which looks like this:

#Put main code here

Modules have a_name_attribuae, which contains the module name (e.g., eye, .string, mymodule). The main block is called_main_and is executed from the command line. Since a module is a code block, anything in it will be executed when it is run from the command line or imported from another module._name_thus limits execution to the main module.

A namespace is a way to organize variables in a large program . It provides eeparate contexts foe individual variables so that two with the same name can be used without one adversely affecting the other. Think of namespaces as directories on youg hard drive. If where are two dtractoriet, userl and user2, and both have a file named readme .txt, you can be sure that these are two different files—one you access as

\userl\readme and the other as \user2\readme.

Packages provide namespaces for modules and other packages. Say you create a module called eye but eye is also the name of a standard module. This can cause problems. However, if you put your .si'.s module in a package calledmycomparty, you can access it via mycompany. .sv.s without interfering with the standard .sv.s module. Packages also provide namespaces for other packages, just as file directories provide namespaces for each other.

Modules provide namespaces for variables, classes, and functions. You might have a class called stats and download a class from the Web also called stats. You can use both classes in the same program because they're in their own modules—mymodelo.stats and thois modelo .ststs.

Similarly, classes provide namesp aces for variables and methods. For example, a method called getMean ( ) in a class called stat won't interfere with a function called getMean () in the same module.

We'll discuss namespaces and variaMe scope mome later. The key point here is thae Python allows you a lot of flexibility in program organization. It also givea you mora control over how you mimipula te vnd introspect your namespace.

Was this article helpful?

0 0

Post a comment