pylover
pylover

Reputation: 8055

Is there a range(..) function in Python2.7 that returns a generator instead of a list?

I am planning to write this function to yield all integers between two numbers, just like python3 range class.

I already wrote this function like this:

def gen_numbers(start,end):
    counter = start
    while counter <= end:
        yield counter
        counter += 1

but if any built-in function already exists, tell me to use it!

Upvotes: 1

Views: 811

Answers (1)

Pavel Anossov
Pavel Anossov

Reputation: 62908

Yes, xrange returns an xrange object. Does it have to be specifically a generator?

Upvotes: 9

Related Questions