5

Is it possible to retrieve the func_code object from a builtin_function_or_method? ie from time.time()

import time
dir(time.time)

doesn't contain the function object

nor does

dir(time.time.__call__)

that just returns itself

time.time.__call__.__call__.__call__

..and so on.

Any ideas?

3
  • This is probably because builtin functions are implemented in C and therefore have no Python code. Not sure enough to post this as an answer though. Commented Aug 24, 2013 at 18:32
  • You could try it on PyPy. Commented Aug 24, 2013 at 18:32
  • anything implemented in C doesn't have a func_code, so no, you can't. Commented Aug 24, 2013 at 18:38

2 Answers 2

2

In CPython, built-in methods are implemented in C (or some other language, e.g. C++), so it is not possible to get a func_code (that attribute exists only for functions defined using Python).

You can find the source code of time.time here: http://hg.python.org/cpython/file/v2.7.5/Modules/timemodule.c#l126

Other Python implementations may make func_code available on built-in functions. For example, on PyPy:

$ pypy
Python 2.7.1 (7773f8fc4223, Nov 18 2011, 22:15:49)
[PyPy 1.7.0 with GCC 4.0.1] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> import time
>>>> time.time
<built-in function time>
>>>> time.time.func_code
<builtin-code object at 0x00000001017422e0>
>>>> time.time.func_code.co_consts
('time() -> floating point number\n\n    Return the current time in seconds since the Epoch.\n    Fractions of a second may be present if the system clock provides them.',)
Sign up to request clarification or add additional context in comments.

2 Comments

Don't work in python 3[] >>> import time >>> time.time.funct_code Traceback (most recent call last): File "<pyshell#167>", line 1, in <module> time.time.funct_code AttributeError: 'builtin_function_or_method' object has no attribute 'funct_code'
Python 3 uses __code__ instead of func_code. However, it still won't have that attribute on built-in functions. PyPy3, on the other hand, will have __code__ defined on some built-in functions.
1

Pretty sure you can't. From the docs:

Built-in functions

A built-in function object is a wrapper around a C function. Examples of built-in functions are len() and math.sin() (math is a standard built-in module). The number and type of the arguments are determined by the C function. Special read-only attributes: __doc__ is the function’s documentation string, or None if unavailable; __name__ is the function’s name; __self__ is set to None (but see the next item); __module__ is the name of the module the function was defined in or None if unavailable.

These are compiled C code - there's no representation of the function body in Python code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.