#!/bin/env python """ global doc string """ def f1(): var = 1 return class myClass: """ myClass doc string """ a = 1 def memFunc(self): pass def main(): print( "myClass.__dict__ %s " % myClass.__dict__) print( "globals: %s" % str( globals())) return if __name__ == "__main__": main() Output: myClass.__dict__ {'a': 1, '__module__': '__main__', '__doc__': ' myClass doc string ', 'memFunc': <function memFunc at 0x7fd7cf4af6e0>} globals: {'f1': <function f1 at 0x7fd7cf4af5f0>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 't1.py', '__package__': None, 'myClass': <class __main__.myClass at 0x7fd7cf49d6d0>, '__name__': '__main__', 'main': <function main at 0x7fd7cf4af668>, '__doc__': '\nglobal doc string\n'}
__name__
variable
is set to __main__
:
if __name__ == "__main__": modMain()
__init__
file is a package.
__init__
file can be empty, but it can also be used to
populate the package name space by import startments.
The following example demonstrates the effects of imports of sub-packages:
PySpectra/__init__.py contains from pqtgrph.graphics import * and PySpectra/pqtgrph/graphics.py contains import PySpectra.misc.zmqIfc as zmqIfc
As a consequence, after import PySpectra the module PySpectra.misc.zmqIfc can be immediately referenced, without further imports.
In [1]: import sys In [2]: sys.path.insert( 0, "/home/userUser/pythonDir") In [3]: sys.path Out[3]: ['/home/userUser/pythonDir', ”, '/usr/bin', '/usr/lib/python2.7', ...]
__name__
.
__builtin__
:
In [4]: import __builtin__ In [5]: dir( __builtin__) Out[5]: ['ArithmeticError', 'AssertionError', ...]