0

I'm trying to look at the source code of the pow() function, and would like to get to know my way around the Python34 directory. I've looked at various libraries in the Lib directory such as fractions.py, and base64.py, but I can't seem to find the .py file where built-in functions are stored. Where should I look?

polarbits
  • 187
  • 1
  • 3
  • 10

2 Answers2

1

Builtins and functions from the standard library are not always implemented in Python. In CPython (the reference Python implementation), they're often written in C

The method you are looking for is defined in the following files (depending on the type you're interested in):

You can actually grep the Objects directory in the CPython source tree for /*nb_power*/ to find more. Try this search query on GitHub.

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • Also see `PyNumber_Power` in http://svn.python.org/projects/python/trunk/Python/bltinmodule.c and `ternary_op` in http://svn.python.org/projects/python/branches/r23a1-branch/Objects/abstract.c – PM 2Ring Apr 20 '15 at 12:39
0

Functions which repr contain the wording "built-in" are not actually defined in Python code, in cPython - they are ratehr written in C, and amde available as binary modules.

For example, when you get the repr of "pow" you get:

>>> repr(pow)
'<built-in function pow>'

In contrast with a Python defined function:

>>> import glob
>>> repr (glob.glob)
'<function glob at 0x7efefe69aa70>'

So, you can search for the C source code of functions marked as "built-in" in the source code for Python itself, if you download it from Python.org (or fetch it with mercurial).

If you want a pure Python implementation of any function in the satandard lib, that should be included with the pypy distribution - just fetch it from:

https://bitbucket.org/pypy/pypy and you should see pure Python implementation of all functions that in cPython are implemented in C for performance or historic reasons.

jsbueno
  • 99,910
  • 10
  • 151
  • 209