0

I have this code:

def generator(n):    list_of = range(1,n+1)    for i in list_of:        if i % 7 == 0:            yield iprint generator(100)

This should print all the numbers in the given range that are divisible by7, but the output is<generator object generator at 0x1004ad280> instead.

Also, the wordyield in my text editor (KOD) doesn't appear highlighted in sky blue color like all the reserved words but instead appears in white, is that fine?

Martijn Pieters's user avatar
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
askedMay 30, 2014 at 11:45
Martin Spasov's user avatar
2
  • It's bug/misfeature in your text editors syntax highlighting.CommentedMay 30, 2014 at 11:50
  • You may also want to have a look at this:stackoverflow.com/q/231767/1025391CommentedMay 30, 2014 at 11:57

3 Answers3

10

Your generatorworks. You forgot to iterate over it though:

for elem in generator(100):    print elem

or you could turn it into a list:

print list(generator(100))

You instead printed the generator object produced by calling the generator function. A generator function produces asuspended generator. Only when you iterate over it is code executed (until the nextyield).

Demo:

>>> def generator(n):...     list_of = range(1,n+1)...     for i in list_of:...         if i % 7 == 0:...             yield i... >>> print list(generator(100))[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]

Thelist() call iterates over the given argument, producing a Python list object with all elements yielded by the argument. Great for iterating over a generator to pull in all the elements that it produces.

As for KOD; that editor hasn't seen updates in years now; you may want to switch to something else. As theKOD twitter feed stated2 years ago:

Don't wait for me, I'm kind of like a Zombie. Go get Sublime Text@sublimehq which is awesome:http://www.sublimetext.com

I concur; Sublime Text is my current editor of choice.

answeredMay 30, 2014 at 11:46
Martijn Pieters's user avatar
Sign up to request clarification or add additional context in comments.

Comments

3

Generators functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. u can learn Here:generator

def generator(n):        list_of = range(1,n+1)        for i in list_of:            if i % 7 == 0:                yield i    for i in generator(100):        print i

or

You can usenext(generator(100)) to print one element at top

or

list(generator(100))
user229044's user avatar
user229044
241k41 gold badges347 silver badges350 bronze badges
answeredMay 30, 2014 at 11:46
sundar nataraj's user avatar

Comments

2

When calling the generator function you receive generator object. In order to get values you should iterate over this object. In you case you can dolist(generator(100))

But this doesn't make sense. Use list comprehension if you need list:

[x for x in range(1, 101) if x % 7 == 0]
answeredMay 30, 2014 at 11:47
Alex Shkop's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.