What's the difference?

A

Anthony Papillion

Someone helped me with some code yesterday and I'm trying to
understand it. The way they wrote it was

subjects = (info[2] for info in items)

Perhaps I'm not truly understanding what this does. Does this do
anything different than if I wrote

for info[2] in items
subject = info[2]

Thanks!
Anthony
 
E

Emile van Sebille

On 6/10/2010 1:47 PM Anthony Papillion said...
Someone helped me with some code yesterday and I'm trying to
understand it. The way they wrote it was

subjects = (info[2] for info in items)

Perhaps I'm not truly understanding what this does. Does this do
anything different than if I wrote

for info[2] in items
subject = info[2]

more like:

result = []
for info in items:
result.append(info[2])

subjects =iter(result)

Emile
 
T

Thomas Jollans

Someone helped me with some code yesterday and I'm trying to
understand it. The way they wrote it was

subjects = (info[2] for info in items)

This is a generator expression, and it creates a generator object. If
you loop over it (subjects), you will get all the subjects (or whatever
info[2] is), one by one.
Perhaps I'm not truly understanding what this does. Does this do
anything different than if I wrote

for info[2] in items
subject = info[2]

These two snippets do the same thing:

# No. 1.
subjects = (info[2] for info in items)
for subject in subjects:
print (subject)

# No. 2.
for info in items:
subject = info[2]
print (subject)


However, you could also pass the subjects variable to another function
and have it iterate over them - that's something that the simple loop
can't do just like that.
 
E

Ethan Furman

Anthony said:
Someone helped me with some code yesterday and I'm trying to
understand it. The way they wrote it was

subjects = (info[2] for info in items)

Perhaps I'm not truly understanding what this does. Does this do
anything different than if I wrote

for info[2] in items
subject = info[2]

Close -- the correct form is

for info in items:
subject = info[2]

Basically, python goes through the elements of items, assigning each one
to the name 'info' during that loop; then in the body of the loop, you
can access the attributes/elements/methods/whatever of the info object.

Hope this helps!

~Ethan~
 
M

Martin

Thank you Emile and Thomas! I appreciate the help. MUCH clearer now.

Also at a guess I think perhaps you wrote the syntax slightly wrong
(square brackets)...you might want to look up "list comprehension"

Martin
 
T

Terry Reedy

Someone helped me with some code yesterday and I'm trying to
understand it. The way they wrote it was

subjects = (info[2] for info in items)

This is more or less equivalent to

def _():
for item in items:
yield info[2]
subjects = _()
del _

Terry Jan Reedy
 
E

Ethan Furman

Ethan said:
Anthony said:
Someone helped me with some code yesterday and I'm trying to
understand it. The way they wrote it was

subjects = (info[2] for info in items)

Perhaps I'm not truly understanding what this does. Does this do
anything different than if I wrote

for info[2] in items
subject = info[2]

Close -- the correct form is

for info in items:
subject = info[2]

Basically, python goes through the elements of items, assigning each one
to the name 'info' during that loop; then in the body of the loop, you
can access the attributes/elements/methods/whatever of the info object.

Hope this helps!

~Ethan~

Ack. My correction only dealt with the info[2] issue on the for line;
Emille's answer properly matches the original code.

Sorry.

~Ethan~
 
S

Steven D'Aprano

Also at a guess I think perhaps you wrote the syntax slightly wrong
(square brackets)...you might want to look up "list comprehension"


You guess wrong. Python also has generator expressions, which are written
using round brackets instead of square.

In short, the syntax:

it = (expr for x in seq if cond)

is equivalent to:

def generator():
for x in seq:
if cond:
yield expr

it = generator()


except it doesn't create the generator function first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top