default variable in python $_

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Tue Oct 10 19:38:32 EDT 2006


rh0dium:
> This is where $_ in perl is awesome - There must be a default variable
> in python right?

A default variable may add bugs to your code, and newbies of the
language may see it coming from air, so Python avoids such things. The
only Python "default variable" I know of is the _ that when used in the
Python shell, it (seem to) stores the last not-None result (inside
scripts it is a normal name sometimes used to 'ignore' some values):

>>> a = 1
>>> _
Traceback (most recent call last):
  ...
NameError: name '_' is not defined
>>> print 2
2
>>> _
Traceback (most recent call last):
  ...
NameError: name '_' is not defined
>>> 5 / 3
1
>>> _
1
>>> None
>>> _
1
>>> 2
2
>>> _
2

Bye,
bearophile




More information about the Python-list mailing list