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']
 
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的`dir()`函数可以列出一个对象所拥有的所有属性和方法。如果没有提供参数,它会返回当前作用域内的所有变量、方法和定义的类型等名称列表。 例如,我们可以使用`dir()`函数来查看一个字符串对象的所有方法: ```python string = "Hello, World!" print(dir(string)) ``` 输出结果: ``` ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] ``` 可以看到,这个字符串对象拥有很多方法,包括大小写转换、查找、替换、分割、字符填充等等。通过使用`dir()`函数,我们可以更好地理解Python对象的方法和属性,从而更好地使用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值