daily pastebin goal
67%
SHARE
TWEET

Untitled

a guest May 16th, 2018 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. """
  4. illustrates Python's dynamic import behavior
  5. """
  6.  
  7. import sys
  8.  
  9.  
  10. def main(args):
  11.     args = [unicode(arg, "UTF-8") for arg in args]
  12.  
  13.     PACKAGE = "package"
  14.     MODULE = "module"
  15.     ATTR = "foo"
  16.  
  17.     if args[1] == "A":
  18.         m = __import__(PACKAGE)
  19.     elif args[1] == "B":
  20.         m = __import__(PACKAGE, fromlist=[MODULE])
  21.     elif args[1] == "C":
  22.         m = __import__("%s.%s" % (PACKAGE, MODULE))
  23.     elif args[1] == "D":
  24.         m = __import__("%s.%s" % (PACKAGE, MODULE), fromlist=[MODULE])
  25.     else:
  26.         raise ValueError
  27.  
  28.     print "%s -- %s --" % (m.__name__,
  29.         m.__name__.endswith(MODULE) or MODULE in dir(m)),
  30.  
  31.     try:
  32.         print getattr(m, ATTR)
  33.     except AttributeError, exc:
  34.         print "AttributeError: %s" % exc
  35.  
  36.     return True
  37.  
  38.  
  39. if __name__ == "__main__":
  40.     status = not main(sys.argv)
  41.     sys.exit(status)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top