getting tut. example to work

J

John Forse

I'm trying some examples from the language ref sections on the site in
the process of learning some python in 3.0.1. I've tried to run both
of the samples below ,but the only printout is "generator object chain
at 0x11f4dc8" whether I use print() or not . How do I use the sample
to produce the expected printout a b c d e f.
Thanks
John F
A
def chain(*iterables):
for it in iterables:
for element in it:
print(element)
yield element

def main():
chain('abc','def')

main()
B
alternative
import itertools
itertools.chain('abc','def')
 
A

alex23

I'm trying some examples from the language ref sections on the site in  
the process of learning some python in 3.0.1. I've tried to run both  
of the samples below ,but the only printout is "generator object chain  
at 0x11f4dc8" whether I use print() or not .

Yes, because you're creating a generator object but you're not
actually doing anything with it.
How do I use the sample  
to produce the expected printout a b c d e f.

Since you have a print inside 'chain', you should be able to get that
output by doing the following:

list(chain('abc','def'))

Although I'd recommend removing the print from 'chain' and doing this
instead:

for elem in chain('abc','def'):
print elem
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top