2

Note: The example output is an ipython (an enhanced python programming console) feature instead of a python 3 standard (@DTing). One can obtain a list of member functions by dir(libname) command when the default console is used (@Matthew Runchey).

In a python 2.7 console, one has the command libname. to print out member functions of an imported library libname . The expected output should be something like:

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

(the example is taken from around 0:13 in this video: https://youtu.be/dG3tOsGEYP4 )

However, my python 3.4 console (Win7 x64) gave a syntax error.

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

So what is the correct way of doing the same thing in python 3?

Bill Huang
  • 4,491
  • 2
  • 13
  • 31

2 Answers2

2
dir(module_name)

This prints a list of the functions in whatever you imported.

>>>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']
Matthew Runchey
  • 234
  • 2
  • 6
1

That is not a feature of python, rather an ipython tab completion feature.

See below:

$ 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
>>>

hitting tab after the .:

$ 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

also works for 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

If you are using ipython, this might be a related issue IPython tab completion not working

Community
  • 1
  • 1
dting
  • 38,604
  • 10
  • 95
  • 114
  • The information is as useful as the answer. I found ipython to be awesome and will replace the default console with it. Thank you very much for the demo. :) – Bill Huang Jun 05 '15 at 08:13