Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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:

print(getattr.__code__)

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.

like image 308
pasghetti Avatar asked Aug 15 '26 13:08

pasghetti


1 Answers

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.

like image 73
Cyphase Avatar answered Aug 18 '26 03:08

Cyphase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!