Manas Chaturvedi Manas Chaturvedi - 6 months ago 19
Python Question

Type of an underscore character in Python

I was playing with the

type()
method in Python, and came across this:

>>> type(_)
<type 'type'>


The 'type' of the underscore( _ ) character is
type
itself. What does that even mean?

Answer

Inside python interpreter, _ is a special variable that returns output from previous line, so depending on the last line type of that variable value could be different.

For example,

>>> type(_)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
>>> 1
1
>>> type(_)
<type 'int'>
>>> type(_)
<type 'type'>
Comments