-1

I'm using CPython 3.6.2, when executing print(dir()) I get:

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

The dir built-in function for example is inside __builtins__, so if only the modules above are shown for print(dir()), I was thinking that to do so python should've done something like:

import __builtins__
from __builtins__ import *

But if that was true I was able to see using print(dir()) the __builtins__ module + dir and other built functions.

So my question is how can I see the pre-loaded modules(using dir()), but not the built-in functions even though I can access them directly(without using __builtins__.dir for example).

Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88
  • It's not very clear what you're asking. You open with a question about functions and `dir`, but then start asking about modules. If you're just looking for what modules are always loaded at startup then do `import sys; print(sys.modules.keys())`. – Dunes Aug 02 '17 at 09:25

2 Answers2

2

This is a result of how Python scope resolution works.

Dir() returns a list of names in the current namespace. So why doesn't it return the builtin functions as well?

Why, because obviously they are not in the current namespace.

Import * would dump all the functions from any module into the current namespace like you'd expect, but that's not necessary to provide access to the builtin functions.

In the same way Python falls back onto global variables if it doesn't find the variable's local definition, inside a class or function or whatever, if it doesn't find a global variable with the name being referenced, it starts looking for it in the builtins module.

TL;DR - why can't you see builtin functions using bare dir()? Because they aren't in the local scope, and they don't need to be there to work.

jkm
  • 46
  • 1
  • So the global scope will only contian: `['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']`, and it's python internal mechanism that will look for any unknown reference(within local scope) inside the `__builtins__` module? – Aviel Fedida Aug 02 '17 at 09:34
  • Yes, no var with that name in local scope -> check enclosing -> check global -> check __builtins__ -> if still not found there throw an error. – jkm Aug 02 '17 at 09:39
1

Quoting docs:

Without arguments, return the list of names in the current local scope.

Built-in functions are not defined in your module scope. Is as simple as that.

Of course, after explicitly importing them, you create names in local scope which points to same objects as names in global scope.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
  • So each module I create will contain `'__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'`, but the global scope that I won't see will have the built-in functions? – Aviel Fedida Aug 02 '17 at 09:29
  • https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules contains look-up rules. There is technically a difference between global scope and builtins, but it's good approximation to treat builtins as "most enclosing" scope. – Łukasz Rogalski Aug 02 '17 at 09:39