Built-in Variables
  1. # Vehicle.py
  2. """ Vehicle Module defines Vehicle class
  3. """
  4.  
  5. class Vehicle(object):
  6. """Document String: Define a Vehicle class"""
  7.  
  8. def __init__(self, brand, year):
  9. self.brand = brand;
  10. self.year = year;
  1. # test.py
  2. """ Test Vehicle class defined in Vehicle module
  3. """
  4.  
  5. from Vehicle import Vehicle
  6.  
  7. def main():
  8. print Vehicle.__bases__ # base classes from which the calss directly inherits
  9. print Vehicle.__dict__ # a dictionary that corresponds to the class's namespace
  10. print Vehicle.__doc__ # a class's docstring
  11. print Vehicle.__module__ # module name in which the class is defined
  12. print Vehicle.__name__ # class name
  13.  
  14. v = Vehicle("Buick", 1999)
  15. print v.__class__ # class name from which the object was instantiated
  16. print v.__dict__ # {'brand': 'Buick', 'year': 1999}
  17.  
  18. if __name__ == '__main__':
  19. main()
__builtins__
  • provides direct access to all ‘built-in’ identifiers of Python; for example, __builtin__.open is the full name for the built-in function open()
  • __name__, the name of the module
    __file__, the “path” to the file
    1. # Vehicle.py
    2. class Vehicle(object):
    3. """Document String: Define a Vehicle class"""
    4.  
    5. def __init__(self, brand, year):
    6. #print "Create Vehicle Object ...";
    7. self._brand = brand;
    8. self._year = year;
    9.  
    10. def main():
    11. v = Vehicle("Lincoln", 1998);
    12. print(__name__) # __main__
    13. print(__file__) # Vehicle.py
    14.  
    15. if __name__ == "__main__":
    16. main()
    __all__
  • Defined in __init__.py file, the list of modules that should be imported by from myPackage import *
  • Reference
  • Index – _
  • Python How to Program, Appendix O