Python Forum
Tuple generator, and function/class syntax
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tuple generator, and function/class syntax
#4
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...
quazirfan likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Tuple generator, and function/class syntax - by buran - Aug-10-2021, 09:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 757 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  [Solved] unkown (to me) function def parm "name1:name2" syntax. MvGulik 5 1,111 Nov-11-2022, 11:21 AM
Last Post: MvGulik
  search a list or tuple for a specific type ot class Skaperen 8 1,980 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  TimeOut a function in a class ? Armandito 1 1,678 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 2,014 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Calling a class from a function jc4d 5 1,863 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  invalid syntax in my class CompleteNewb 2 1,945 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,992 Oct-06-2021, 09:39 AM
Last Post: menator01
  a function common to methods of a class Skaperen 7 2,672 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,126 Apr-30-2021, 07:37 PM
Last Post: quest

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020