Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have this code snippet which I can't understand:

>>> l = lambda: -4, 'c', 0
>>> i = iter(l)
>>> i
<tuple_iterator object at 0x00700CD0>
>>> next(i)
<function <lambda> at 0x0070A4F8>
>>> next(i)
'c'
>>> next(i)
0
>>> next(i)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>>
Why is it returning lambda object on first iteration, instead of -4?

1 Answer

0 votes
by (25.1k points)

In your code l is tuple with 3 elements: A lambda that returns -4, a string ‘c’, and an integer 0.

The reason that it returns lambda object on first iteration and not -4 is because you need to call the lambda by using parentheses like:

next(i)()

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...