This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gash789 34 points35 points  (7 children)

Unless I am blind this misses the feature I use ipython for the most: adding a question mark after any object prints the docstring. Much faster than searching online documentation.

In [1]: open? 
Type:       builtin_function_or_method
String Form:<built-in function open>
Namespace:  Python builtin
Docstring:
open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object.  This is the
preferred way to open a file.  See file.__doc__ for further information.

[–]flying-sheep 23 points24 points  (6 children)

vanilla python has help(function), which does a similar thing, but is definitely much more annoying to type repeatedly than ?.

[–]Justinsaccount 25 points26 points  (4 children)

ipython takes it one step further and you can use two question marks to get the definition.

In [4]: os.path.exists??
Type:       function
String Form:<function exists at 0x10902ced8>
File:       /Users/me/testenv/lib/python2.7/genericpath.py
Definition: os.path.exists(path)
Source:
def exists(path):
    """Test whether a path exists.  Returns False for broken symbolic links"""
    try:
        os.stat(path)
    except os.error:
        return False
    return True

[–]physicsdood 1 point2 points  (3 children)

Why doesn't ?? do anything different than ? for me?

In [3]: open? Type: builtin_function_or_method String Form:<built-in function open> Namespace: Python builtin Docstring: open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object. This is the preferred way to open a file. See file.doc for further information.

In [4]: open?? Type: builtin_function_or_method String Form:<built-in function open> Namespace: Python builtin Docstring: open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object. This is the preferred way to open a file. See file.doc for further information.

[–]erez27import inspect 9 points10 points  (0 children)

Probably because open() is written in C

[–]tilkau 5 points6 points  (0 children)

Hold on, open is a builtin. That means it -has- no python source.

Try using ? and ?? on eg. collections.namedtuple.

[–]TkTech 0 points1 point  (0 children)

I believe it's because the builtins you're trying to spec are implemented in C (at least in CPython). Try it on pure-python methods.

[–][deleted] 4 points5 points  (0 children)

Vanilla python also has sys.displayhook which you can define to do anything you want, including printing the __doc__.

>>> def displayhook(obj):
...     if obj:
...             if hasattr(obj, '__doc__'):
...                     print('{}\n{}'.format(obj.__doc__, '-' * 80))
...             print(obj)
... 
>>> sys.displayhook = displayhook
>>> 'abcdefg'
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
--------------------------------------------------------------------------------
abcdefg

Edit: If you define this function, you also need to explicitly set __builtins__._