pasghetti pasghetti - 9 months ago 47
Python Question

Why don't Python's built-in functions have code objects?

I'm fairly new to python, so this might be an obvious question, but I searched everywhere (including the documentation), and I can't seem to get the code object of a builtin function in python. For example, in my PyCharm editor, when I type this code, it does not detect any errors:


But when I run it, it gives an AttributeError:

AttributeError: 'builtin_function_or_method' object has no attribute '__code__'

Is there a reason why built-in functions in python do not have a code object, and is this a bug in my IDE?

I am using Python 3.

Answer Source

Built-in functions don't have code objects because they're implemented in C, not in Python. Only Python functions have code objects, because code objects are compiled Python byte-code. Exploring Python Code Objects is a good explanation of code objects.