Example analysis of xrange and yield in Python

  • 2020-06-19 10:57:14
  • OfStack

This paper analyzes the usage of xrange and yield in Python. To share for your reference, specific as follows:

range and xrange

Python provides built-in functions to generate and return sequences of integers, range and xrange, which are functionally similar but implemented differently. range(n, m) Returns a list of consecutive integers from n to (m-1) xrange(n, m) Instead, a special destination object, the xrange object itself, is returned.


>>> range(1, 5)
[1, 2, 3, 4]
>>> xrange(1, 5)
xrange(1, 5)
>>> type(xrange(1, 5))
<type 'xrange'>

But in python2.x, xrange does not return an iterator, so x = xrange(n, m), x.next() Will go wrong. If you want to return an iterator, you need to call iter(xrange(... .))


>>> x = iter(xrange(1, 5))
>>> x.next()
1
>>> x.next()
2

That is, calling THE range and xrange programs does not occupy the same amount of memory during execution. With range, the program will first generate an list and then implicitly call iter of list to get the element. With xrange, the program generates 1 xrange object per loop. This object is iterable, and we can retrieve the elements based on the returned xrange object.

Generator with yield

With python's generator, we can implement a generator like the built-in xrange function, but this generator returns a sequence of 1 and floating point values instead of an integer sequence.


>>> def frange(start, stop, step=1.0):
  while start < stop:
    yield start
    start += step
>>> frange(1.0, 5.0)
<generator object frange at 0x01343148>
>>> for i in frange(1.0, 5.0):
  print i,
1.0 2.0 3.0 4.0
>>> x = iter(frange(1.0, 5.0))
>>> x.next()
1.0
>>> x.next()
2.0

In python, one or more yield appear in the body of the function, which is the generator (generator). When a generator is called, the body of the generator function is not executed. When called, the generator returns a special iterator object containing the generator function body, its local variables (including its parameters), and its current execution location.

When the next method of the returned iterator object is called, the generator executes the next yield statement.

At the end of the yield statement, the execution of the function is "frozen," retaining the current position of execution and unused local variables, and returning the result of the yield statement as the result of the next method. If you continue calling next, you continue calling yield until either the body of the function has finished running or the return statement has been executed (return statements cannot contain expressions).

Most commonly, generators can be used to build iterators. If we need a sequence of Numbers from 1 to N and then from N to 1, we can use the generator:


>>> def updown(N):
  for x in xrange(1, N): yield x
  for x in xrange(N, 0, -1): yield x
>>> for i in updown(5):
  print i,

Generators can be more flexible when a function needs to return a list. A generator can build a misunderstood iterator and return an infinite sequence of results. One step further, the iterator built by the generator performs lazy calculations: the results are only calculated when the function needs them.

So if you need to iterate over a sequence, consider iterators.

For more information about Python, please refer to Python Mathematical Operation Skills Summary, Python Data Structure and Algorithm Tutorial, Python Function Using Skills Summary, Python String Operation Skills Summary, Python Introduction and Advanced Classic Tutorial and Python File and Directory Operation Skills Summary.

I hope this article has been helpful in Python programming.


Related articles: