Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of an underscore character in Python [duplicate]

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?

like image 802
Manas Chaturvedi Avatar asked Jan 07 '23 14:01

Manas Chaturvedi


1 Answers

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'>
like image 99
Marcin Pietraszek Avatar answered Jan 21 '23 19:01

Marcin Pietraszek