Python使用技巧(持续更新)

1、dir()和help()
    dir()如果没有输入参数的话,返回当前范围内定义的模块、变量、方法;如果有输入参数的话,则返回输入参数包含的方法和属性。例如:
 

>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a']
>>> dir({})
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>>

    help()可以查看模块信息、类信息、方法信息。例如:
 

>>> help(sys)

>>> help(sys.path)

>>> help(sys.path)

>>> help(sys.path.__add__)

>>> help(sys.path)

>>> help(sys.path)

>>> help(sys.path.clear)

总结:因为模块、类、方法很多,不可能都记得住详细信息和用法,善用dir()方法查出有什么类、什么方法,再用help()查出具体怎么使用。

猜你喜欢

转载自blog.csdn.net/Michael_Zheng_Tech/article/details/82827715