Search code examples
pythonfunctionpython-2.7pointersmonkeypatching

What is the function pointer of the print builtin function


How can we get the function pointer of the builtin print function in python.

It seems to behave differently than the other builtin functions:

>>> a = print
SyntaxError: invalid syntax

>>>> dir(print)
SyntaxError: invalid syntax

>>>> m = map
OK

>>>> dir(map)
['__call__', '__class__', [...] '__str__', '__subclasshook__']

Solution

  • You can't do this because print is a keyword in Python 2.7, so it would be along the lines of saying something like:

    >>> a = if
    

    Doesn't make too much sense.

    You have two options.

    1. Use python 3

    2. Import the python equivalent: from __future__ import print_function