三个很有用的函数
我在不同的场合强调过这3个学习者必须知道的3个函数。
因为知道了它们,你就具备了一定的元学习能力!在没有人帮你的时候,也可以高效的自己探索学习。
这三个函数就是:
- dir()
- help()
- id()
假设有位高人跟你说:你可以用random模块产生随机数字。
你内心充满感谢,但仍然会很懵逼。因为你不知道怎么用random模块。
dir就是打开这个问题的正确姿势:
import random
print(dir(random))
上面的代码使用dir函数列出random模块下的所有变量和函数,然后打印出来。运行结果:
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_ONE', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_floor', '_index', '_inst', '_isfinite', '_log', '_os', '_pi', '_random', '_repeat',<