def iter_latest_block(w3, to_block=None):
    """Returns a generator that dispenses the latest block, if
    any new blocks have been mined since last iteration.

    If there are no new blocks None is returned.

    If ``to_block`` is defined, ``StopIteration`` is raised
    after to_block is reached.

    >>> mined_blocks = dispense_mined_blocks(w3, 0, 10)
    >>> next(new_blocks)  # Latest block = 0
    0
    >>> next(new_blocks)  # No new blocks
    >>> next(new_blocks)  # Latest block = 1
    1
    >>> next(new_blocks)  # Latest block = 10
    10
    >>> next(new_blocks)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      StopIteration
    >>>
    """
    _last = None
    is_bounded_range = to_block is not None and to_block is not 'latest'
    while True:
        latest_block = w3.eth.blockNumber
        if is_bounded_range and latest_block > to_block:
            return
        if _last is not None and _last == latest_block:
            yield None
        else:
            yield latest_block
        _last = latest_block
