Python的dir()函数

1、内置的函数 dir() 可以找到模块内定义的所有名称。以一个字符串列表的形式返回。
  1. >>>import fibo,sys
  2. >>> sys.path
  3. ['E:\\python\\work','D:\\Python36\\Lib\\idlelib','D:\\Python36\\python36.zip','D:\\Python36\\DLLs','D:\\Python36\\lib','D:\\Python36','D:\\Python36\\lib\\site-packages']
  4. >>> dir(sys)
  5. ['__displayhook__','__doc__','__excepthook__','__interactivehook__','__loader__','__name__','__package__','__spec__','__stderr__','__stdin__','__stdout__','_clear_type_cache','_current_frames','_debugmallocstats','_enablelegacywindowsfsencoding','_getframe','_git','_home','_xoptions','api_version','argv','base_exec_prefix','base_prefix','builtin_module_names','byteorder','call_tracing','callstats','copyright','displayhook','dllhandle','dont_write_bytecode','exc_info','excepthook','exec_prefix','executable','exit','flags','float_info','float_repr_style','get_asyncgen_hooks','get_coroutine_wrapper','getallocatedblocks','getcheckinterval','getdefaultencoding','getfilesystemencodeerrors','getfilesystemencoding','getprofile','getrecursionlimit','getrefcount','getsizeof','getswitchinterval','gettrace','getwindowsversion','hash_info','hexversion','implementation','int_info','intern','is_finalizing','maxsize','maxunicode','meta_path','modules','path','path_hooks','path_importer_cache','platform','prefix','set_asyncgen_hooks','set_coroutine_wrapper','setcheckinterval','setprofile','setrecursionlimit','setswitchinterval','settrace','stderr','stdin','stdout','thread_info','version','version_info','warnoptions','winver']
  6. >>> dir(fibo)
  7. ['__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package__','__spec__','fib','fib2']
2、如果没有给定参数,那么 dir() 函数会罗列出当前定义的所有名称。
  1. >>> a =[1,2,3,4,5]
  2. >>>import fibo
  3. >>> fib = fibo.fib
  4. >>> dir()
  5. ['__annotations__','__builtins__','__doc__','__file__','__loader__','__name__','__package__','__spec__','a','fib','fibo','sys']
  6. >>> a =5
  7. >>> dir()
  8. ['__annotations__','__builtins__','__doc__','__file__','__loader__','__name__','__package__','__spec__','a','fib','fibo','sys']
  9. >>>del a
  10. >>> dir
  11. <built-in function dir>
  12. >>> dir()
  13. ['__annotations__','__builtins__','__doc__','__file__','__loader__','__name__','__package__','__spec__','fib','fibo','sys']
 

猜你喜欢

转载自cakin24.iteye.com/blog/2382323