Python Forum
Tuple generator, and function/class syntax - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Tuple generator, and function/class syntax (/thread-34568.html)



Tuple generator, and function/class syntax - quazirfan - Aug-10-2021

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.
[ 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?


RE: Tuple generator, and function/class syntax - deanhystad - Aug-10-2021

A comprehension is a tight little loop that loads a mutable container type (lists, sets, dictionaries) with values. A tuple is an immutable type. Once created it cannot be modified. A tuple comprehension is an impossibility.


RE: Tuple generator, and function/class syntax - quazirfan - Aug-10-2021

Sorry, I am not following your logic.

(Aug-10-2021, 02:35 AM)deanhystad Wrote: Once created it cannot be modified.

Why not instantiate it during creation? For example, why not treat ( x for x in range(10)) as (10, 10, 10, 10, 10, 10, 10, 10, 10, 10)?


RE: Tuple generator, and function/class syntax - buran - Aug-10-2021

There is no such thing as "tuple comprehension". This (x for x in range(100000000)) is generator expression.

Read Generator expressions and list comprehensions and also PEP-0289 for more details.

That said, you need to know that in python 3 range() is lazy and will return range object, i.e. range(100000000) will produce range object and it will be consumed when needed, not generate all numbers in memory (like in python2, where we have lazy xrange()). So basically you create [lazy] generator from lazy range object

you can do list(range(100000000)), set(range(100000000)) and tuple(range(100000000)) to produce list, set or tuple respectively.

(Aug-10-2021, 03:28 AM)quazirfan Wrote: Why not instantiate it during creation? For example, why not treat (x for x in range(10)) as (10, 10, 10, 10, 10, 10, 10, 10, 10, 10)

in any case this will never be (10, 10, 10, 10, 10, 10, 10, 10, 10, 10). it will be (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) if it was a tuple and not generator expression...