Using modules

PEP 302 - New Import Hooks

INTRODUCTION

Why are we using modules?
(a) To reuse code.
(b) To keep the core language small.
(c) To make a safe namespace.
(d) For shared services and data.

A Python program consists of the top-level module and zero or more additional modules.

'__main__' is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt [see locals()].

A module can discover whether or not it is running in the main scope by checking its own __name__.


File name: 'starwars.py'
Module name: 'starwars'
__name__ == '__main__' if the module is running in the main scope
__name__ == 'starwars' if the module is imported