Introducing Python © Chirag Wazir 2002 Prev   Index   Next

Modules & Packages

import, from import, import as

from module import * doesn't import names beginning with _

The dir() function

dir()
    ['__builtins__', '__doc__', '__name__']
import random
dir()
    ['__builtins__', '__doc__', '__name__', 'random']
dir(random)
    ['LOG4', 'NV_MAGICCONST', 'Random', 'SG_MAGICCONST',
     'TWOPI', '__all__', '__builtins__', '__doc__',
     '__file__', '__name__', '_acos', '_cos', '_e',
     '_exp', '_inst', '_log', '_pi', '_sin', '_sqrt',
     '_test', '_test_generator', 'betavariate', 'choice',
     'cunifvariate', 'expovariate', 'gammavariate',
     'gauss', 'getstate', 'jumpahead', 'lognormvariate',
     'normalvariate', 'paretovariate', 'randint', 'random',
     'randrange', 'seed', 'setstate', 'shuffle', 'stdgamma',
     'uniform', 'vonmisesvariate', 'weibullvariate', 'whseed']
dir(random.uniform)
    ['__call__', '__class__', '__cmp__', '__delattr__',
     '__get__', '__getattribute__', '__hash__', '__init__',
     '__new__', '__reduce__', '__repr__', '__setattr__',
     '__str__', 'im_class', 'im_func', 'im_self']
help('random.uniform')
    Help on method uniform in random:
    uniform(self, a, b) method of random.Random instance
        Get a random number in the range [a, b).
random.uniform.__doc__
    'Get a random number in the range [a, b).'
help('random')

Packages are a way of structuring Python's module namespace by using dotted module names

__init__.py files are required to make Python treat the directories as containing packages

Standard Libraries: