I was looking for solutions to take multiline input in python. I found this answer which uses the following code.
sentinel = '' # ends when this string is seen
for line in iter(input, sentinel):
pass # do things here
I read from the python docs that if iter
recieves the second argument then it will call the __next__()
of the first argument. But I don't think input
has the __next__()
implemented (I am not able to verify this either through the docs or surfing through the source code). Can someone explain how it's working?
Also, I observed this weird behaviour with the following code.
sentinel = ''
itr = iter(input, sentinel)
print("Hello")
print(set(itr))
Here is the output
[dvsingla Documents]$ python3 temp.py
Hello
lksfjal
falkja
aldfj
{' aldfj', 'falkja', 'lksfjal'}
[dvsingla Documents]$
The prompt starts taking input after printing Hello which is not following line by line interpretation.
Thanks for any help