Search code examples
pythonpython-idle

How to show calltips in IDLE?


When writing code in IDLE, sometimes when I insert a function like re.sub( in the example, a window pops up explaining the function and the inputs it needs. I find this very helpful and would like to have this window pop up every time. I googled and tried different key combinations but I can't see to find how you do this.

Can somebody help me with this?

enter image description here


Solution

  • Your question is specific to the python IDLE. In IDLE, you have this functionality enabled by default. For it to work, the function (or method) has to be available in the current namespace.That means it has to either be defined in the running environment, or imported in to the running environment.

    For example:

    >>> def foo(x)
            """the foo function"""
            return x
    

    when you type >>> foo( in the prompt after the definition, you will see the explanation which really is the documentation contained in the docstring (the stuff between the triple quotes).

    If a function or method does not have any documentation, then you will not see any explanation. For example

    >>> def bar(y):
            return y
    

    In this case when you type in bar( at the prompt, IDLE will just show y, this is because the function does not have any documentation.

    Some built in functions (called builtins) do not have docstrings, often this is because they are implemented in the C programming language. For example

    >>> from functools import reduce
    >>> reduce(
    

    In this case IDLE will not give any hint because the function does not have any docstring for it to display.

    A great companion to learning is the python standard reference. You can lookup built in function definitions there for clear explanations about what they do. On the other hand, when writing your own functions, remember to put docstrings as they will help you as you go on.