3

Can I get python to print the source code for __builtins__ directly?

OR (more preferably):

What is the pathname of the source code for __builtins__?


I at least know the following things:

  • __builtins__ is a module, by typing type(__builtins__).

  • I have tried the best-answer-suggestions to a more general case of this SO question: "Finding the source code for built-in Python functions?". But no luck:

    • print inspect.getdoc(__builtins__) just gives me a description.

    • inspect.getfile(__builtins__) just gives me an error: TypeError: <module '__builtin__' (built-in)> is a built-in module

    • https://hg.python.org/cpython/file/c6880edaf6f3/# does not seem to contain an entry for __builtins__. I've tried "site:" search and browsed several of the directories but gave up after a few.

4
  • @MooingRawr I have tried the answers in that original post Commented Nov 28, 2016 at 19:25
  • should have mention that... then .... Commented Nov 28, 2016 at 19:26
  • @MooingRawr, cheers, my bad will edit Commented Nov 28, 2016 at 19:27
  • Note: the __builtins__ name is an implementation detail, and __builtins__ does not behave as consistently as you might expect. For example, it's not always a module! If you want to access the module where built-in names are defined, you should import __builtin__ (no s), or import builtins (no underscores) on Python 3. Commented Nov 28, 2016 at 21:23

2 Answers 2

6

The __builtin__ module is implemented in Python/bltinmodule.c, a rather unusual location for a rather unusual module.

Sign up to request clarification or add additional context in comments.

3 Comments

Cheers, how did you know this? or how did you work it out?
@Oracle: I've just spent a lot of time looking through the Python source code, so I'm familiar with where a lot of things are.
Respect. At least I know there isn't some tool I'm missing out on (maybe).
-2

I can't try it right now, but python default ide is able to open core modules easily (I tried with math and some more)

https://docs.python.org/2/library/idle.html

On menus. Open module.

6 Comments

Could you give a example of what you mean?
I don't understand: "I can try it right now" (try what?), "but" (are you sure you mean but?), "open core modules easily" (yes I bet it can, but how? what line of code?)
I'm writing on the phone, and sadly I can't use idle here :) I edited to clarify what I mean.
Ah IDLE, well I probably have IDLE installed (I use sublime) but it would really be nice to do this without a GUI and I'm guessing it would just give me a huge list to choose from. Because my sys.path is quite huge.
Does this display the real source code? I've seen something similar in PyCharm that just fabricates code that kind of looks like it could have been the source code.
|

Your Answer

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