【Python】dir函数 & help函数

dir函数

dir()函数,可以让我们知道一个包里有什么py文件

import torch dir(torch) 

输出:

['AVG',  'AggregationType',  'AliasDb',  'AnyType',  'Argument',  'ArgumentSpec',  'BFloat16Storage',  'BFloat16Tensor',  'BenchmarkConfig',  'BenchmarkExecutionStats',  'Block',  'BoolStorage',  'BoolTensor',  ...,  'row_stack',  'rrelu',  'rrelu_',  'rsqrt',  'rsqrt_',  ...] 

输入:

dir(torch.cuda.is_available) 

输出:

['__annotations__',  '__call__',  '__class__',  '__closure__',  '__code__',  '__defaults__',  '__delattr__',  '__dict__',  '__dir__',  '__doc__',  '__eq__',  '__format__',  '__ge__',  '__get__',  '__getattribute__',  '__globals__',  '__gt__',  '__hash__',  '__init__',  '__init_subclass__',  '__kwdefaults__',  '__le__',  '__lt__',  '__module__',  '__name__',  '__ne__',  '__new__',  '__qualname__',  '__reduce__',  '__reduce_ex__',  '__repr__',  '__setattr__',  '__sizeof__',  '__str__',  '__subclasshook__'] 

前后都有双下划线意味着这是一种特殊规范,不能去修改这个变量名,这也说明了当前dir函数的实参是一个函数名,我们对这个is_available函数名可以传入help函数,来了解其如何使用。

help函数

help()函数,可以让我们知道每个包是如何使用的,以及使用方法,可以看到官方解释文档

help(torch.cuda.is_available)  # 此处函数不用加() 

输出:

Help on function is_available in module torch.cuda: is_available() -> bool     Returns a bool indicating if CUDA is currently available.