6

First line once Python 2.7 interpreter is started on Windows:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']

Having entered the dir() command, the special variable _ should be defined:

>>> _
['__builtins__', '__doc__', '__name__', '__package__']

But, even after entering _, it does not show up when I attempt to list all names in the interactive namespace using dir():

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']

How does the interpreter recognize this variable if it is not in the interpreter's namespace?

bcf
  • 2,104
  • 1
  • 24
  • 43

1 Answers1

10

_ goes in the built-in namespace, not the globals.

>>> import __builtin__
>>> 3
3
>>> __builtin__._
3

dir() doesn't list built-ins:

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

The built-in scope is a different scope from the one you're running dir() in.

user2357112
  • 260,549
  • 28
  • 431
  • 505