Here is some saved interaction in command prompt.

>>> type((1,2))
<type 'tuple'>
>>> (3*x for x in (4,4))
<generator object <genexpr> at 0x00E73EB8>
>>> tuple(x**2 for x in range(10))
(0, 1, 4, 9, 16, 25, 36, 49, 64, 81)
>>> set(x for x in range(10))
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> {x:x**2 for x in range(10)}
SyntaxError: invalid syntax
>>> x=2
>>> {x:x**2}
{2: 4}
>>> [{x:x**2} for x in range(10)]
[{0: 0}, {1: 1}, {2: 4}, {3: 9}, {4: 16}, {5: 25}, {6: 36}, {7: 49}, {8: 64}, {9: 81}]
>>> type([{x:x**2} for x in range(10)])
<type 'list'>
>>> dict([[x:x**2] for x in range(10)])
SyntaxError: invalid syntax
>>> dict([[x,x**2] for x in range(10)])
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
>>>

Dani AI

Generated

’s interactive trace is a useful, hands‑on tour of generator expressions, container constructors and how Python’s syntax evolved across releases. The behaviors seen in the log come from Python 2.x-era semantics (generator expressions are lazy iterators; dict/set literal comprehensions were introduced later; range/xrange semantics changed in Python 3). (peps.python.org)

Practical clarification about generator expressions: they produce lazy, single‑use iterator objects (they do not automatically create a stored list). For cases that need multiple independent passes, either materialize the sequence into a container (list or tuple) or split the stream using itertools.tee — but tee buffers values and so can increase memory use if one branch runs far ahead. Example pattern (different from the original log) that creates two independent iterators from a generator function:

from itertools import tee

def nums():
    for n in range(6):
        yield n*n

g1, g2 = tee(nums(), 2)
print(list(g1))
print(list(g2))

See PEP 289 for generator expression semantics and the itertools docs for tee caveats. (peps.python.org)

About dict/set comprehensions and older interpreters: the literal form {k: v for ...} (and set comprehensions {x for ...}) became part of the language in later releases; older 2.x interpreters raise SyntaxError. When using an older interpreter, construct a mapping with the dict constructor from an iterable of pairs instead, or upgrade to Python 2.7+/3.x for the literal syntax. (peps.python.org)

Quick checklist from the thread: (1) treat generator expressions as single‑use unless explicitly copied; (2) materialize or tee when reuse is needed, watching memory; (3) prefer modern comprehension syntax on 2.7+/3.x for clarity; (4) remember range/xrange differences when porting between versions. (docs.python.org)

I got one down vote for this post, so I assume that somebody thought:

Oh, heck. This goes totally over my head.

and gave me down vote for missing of clarity.

The motivation to send this post was to show how many interesting features there are in understanding Pythons way of doing things.

So let's take that first one, which came up many times in my programs, when suddenly there was generators included as members of the list, yes that is possible, Pythons types are dynamic and multiple types of object can be put in list.

Are you advocate of object oriented design and have all your programs with main program:

obj=myobject()
myobject,run()

So you like to make list of list of your program file names and instance of man object of each file? No problem 1, 'a', or myspreadsheet(), all you can stuff in list, dict, you name it (there does exist some limitations which type of element set can have for example or dict index must have order, so no set indexes, thank you.

So let's take this first guy, code object called generator. Ever put range() in your for loops? This is one object that can replace that range. Why? Because range is not generator but still is one method to make a list with specified numbers. And list type belongs to class of types called iterables. What makes an iterable type? Only that it must have one method to generate next element from its members, that is generator called iterator.

Don't trust me? Here is the proof:

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=(3*x for x in (4,4))
>>> for i in a: print i
...
12
12
>>> tuple(a)
(12, 12)
>>> b= range(4)
>>> b
[0, 1, 2, 3]
>>> type(range)
<type 'builtin_function_or_method'>
>>> type(range(4))
<type 'list'>
>>>

There, does it make sense to you. No. If it does not matter to you, fine. Don't worry, be happy :icon_biggrin:

If it does, here is some homework to you. What happens here:

>>> a=(3*x for x in (4,4))
>>> a
<generator object <genexpr> at 0x00C6BDC8>
>>> tuple(a)
(12, 12)
>>> tuple(a)
()
>>> b=a
>>> tuple(b)
()
>>> b
<generator object <genexpr> at 0x00C6BDC8>
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.