品易云推流 关闭
文章详情页
文章 > Python基础教程 > dir()函数在python模块的使用

dir()函数在python模块的使用

头像

小妮浅浅

2021-05-28 09:47:523608浏览 · 0收藏 · 0评论

1、说明

内建函数dir()可以找到在模块中定义的所有名称,并作为字符串列表返回。

如果没有给定参数,dir()函数罗列当前定义的所有名称。

2、实例

import Titan
 
print(dir(Titan))
 
# 输出结果:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'age', 'name', 'sayBad', 'sayGood', 'sayNice']
 
 
print(dir())
# 输出结果:
['Titan', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
 
 
# 这里定义一个新的变量
sum = 30
print(dir())
# 输出结果:
['Titan', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sum']
 
 
# 把定义的变量删除后
del sum
print(dir())
# 输出结果:
['Titan', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

以上就是dir()函数在python模块的使用,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图