Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the last command variable "_" appear in dir()? [duplicate]

Tags:

python

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?

like image 363
bcf Avatar asked Nov 30 '17 20:11

bcf


1 Answers

_ 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.

like image 165
user2357112 supports Monica Avatar answered Sep 28 '22 10:09

user2357112 supports Monica