如何在python 3控制台中打印导入库的成员函数?

时间:2015-06-05 07:39:01

标签: python-3.x console

注意:示例输出是ipython(增强的python编程控制台)功能,而不是python 3标准(@DTing)。使用默认控制台时,可以通过dir(libname)命令获取成员函数列表(@Matthew Runchey)。

在python 2.7控制台中,有一个命令libname.可以打印出导入库libname的成员函数。 预期的输出应该是这样的:

>>> import hashlib
>>> hashlib.
hashlib.algorithms  hashlib.new     hashlib.sha224   hashlib.sha384
hashlib.md5         hashlib.sha1    hashlib.sha256   hashlib.sha512

(此视频取自此视频中的0:13左右:https://youtu.be/dG3tOsGEYP4

然而,我的python 3.4控制台(Win7 x64)给出了语法错误。

>>> import hashlib
>>> hashlib.
  File "<stdin>", line 1
    hashlib.
           ^
SyntaxError: invalid syntax

那么在python 3中做同样事情的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

这不是python的一个功能,而是一个ipython选项卡完成功能。

见下文:

$ python
Python 2.7.9 (default, Apr  7 2015, 07:58:25)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> hashlib.
  File "<stdin>", line 1
    hashlib.
           ^
SyntaxError: invalid syntax
>>>

点击.后的标签:

$ ipython
Python 2.7.9 (default, Apr  7 2015, 07:58:25)
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import hashlib

In [2]: hashlib.
hashlib.algorithms             hashlib.md5                    hashlib.sha1                   hashlib.sha384
hashlib.algorithms_available   hashlib.new                    hashlib.sha224                 hashlib.sha512
hashlib.algorithms_guaranteed  hashlib.pbkdf2_hmac            hashlib.sha256

也适用于python3:

$ ipython
Python 3.4.3 (default, Apr  7 2015, 08:05:21)
Type "copyright", "credits" or "license" for more information.

IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import hashlib

In [2]: hashlib.
hashlib.algorithms_available   hashlib.new                    hashlib.sha224                 hashlib.sha512
hashlib.algorithms_guaranteed  hashlib.pbkdf2_hmac            hashlib.sha256
hashlib.md5                    hashlib.sha1                   hashlib.sha384

如果您使用的是ipython,则可能是相关问题IPython tab completion not working

答案 1 :(得分:1)

dir(module_name)

这将打印您导入的任何函数列表。

>>>import hashlib
>>>dir(hashlib)
['__all__', '__builtin_constructor_cache', '__builtins__', '__cached__', '__doc__', '__file__', '__get_builtin_constructor', '__loader__', '__name__', '__package__', '__spec__', '_hashlib', 'algorithms_available', 'algorithms_guaranteed', 'md5', 'new', 'pbkdf2_hmac', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']