The elements within a module - variables, functions, other imports - are referred to as "module attributes". The "import" keyword is used to search for the named module and load it if found. Once the module has been successfully imported, functions and variables contained within it can be accessed by prefixing their names with the module name. Since each module sets up an independent namespace, this is a handy way to avoid name clashes. To see how this works, consider the following example, which uses two modules, each containing a function named rock():
Let's see what happens when I try to use these in a program:
Since each module attribute has a unique prefix, it's possible to distinguish between attributes with the same name in different modules, and thereby avoid name conflicts. You'll be interested to know that everything you do in Python takes place within the context of a module. Even commands typed into the interactive command-line interpreter take place within the bubble of Python's top-level module, named "__main__".
blog comments powered by Disqus |