A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 3 years ago
Hey, can someone explain to me what a python generator is and how to use it?I need to know it to solve a problem.Thanks
anonymous
 3 years ago
Hey, can someone explain to me what a python generator is and how to use it?I need to know it to solve a problem.Thanks

This Question is Open

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0http://docs.python.org/2.7/howto/functional.html#generators http://docs.python.org/2.7/reference/simple_stmts.html#theyieldstatement http://docs.python.org/2.7/library/stdtypes.html#generatortypes http://docs.python.org/2.7/reference/expressions.html#yieldexpressions http://docs.python.org/2.7/howto/functional.html#generatorexpressionsandlistcomprehensions

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0Here's a fibonacci generator. def fib(n): a,b = 0,1 for _ in xrange(n): yield b a,b = b, a+b And a test print fib(10) print list(fib(10)) for i in fib(10): print i, And the output <generator object fib at 0x0298F490> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 1 1 2 3 5 8 13 21 34 55
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.