Aug-10-2021, 01:32 AM
Question 1: I know that I can generate a list of some numbers using list comprehension. And the same syntax works with set. This is not a lazy operation so the following code might take a while to complete.
Question 2: As I am learning python, I was under the assumption
So, when I see a code that appears like
[ x for x in range(100000000)] { x for x in range(100000000)}But when I try to generate a tuple of some numbers using comprehension, it uses generator, and therefore lazy operation. The following code returns immediately.
( x for x in range(100000000))I know that I can use
tuple(x for x in range(100000000))
to get the expected result, but I am wondering why () triggers generator, but not with with [] and {}.Question 2: As I am learning python, I was under the assumption
range()
is a generator function. But upon reading the python documentation I realized it is a class, not a function.So, when I see a code that appears like
something()
- there is no way for me to find out if I am looking at a class instantiation or a function call without looking at the definition of something
- Am I correct?