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

438次阅读
没有评论

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

python dir()函数说明

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

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

python dir()函数实例

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__']
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-30发表,共计795字。
新手QQ群:570568346,欢迎进群讨论 Python51学习