Code Monkey home page Code Monkey logo

epai_session11's Introduction

Iteratables and Iterators

  • Iterables don't return self when iter() method is used externally. The id of the object passed to the iter() method and the id of object would be different since self is not returned for iterable.

  • Iterator's iter function is going to return itself. Iterators are exhaustable, they have both next and iter methods. To make the iterator non-exhaustable, we can use a iterator factory;

Field Iterators Iterables
iter Yes Yes
next Yes Not necessarily required
  • Added below Iterator factory to make existing code a iterable.
class PolygonSequence:

...

    def __iter__(self):
        return self.PolygonSeqIterator(self)

    class PolygonSeqIterator:
        def __init__(self, pseq_obj):
            self.pseq_obj = pseq_obj
            self.index = 0

        def __next__(self):
            if self.index >= len(self.pseq_obj):
                raise StopIteration
            else:
                polygon = self.pseq_obj.sequence[self.index]
                self.index += 1
                return polygon

        def __iter__(self):
            return self

Below are additional test cases:

    p = PolygonSequence(n, R)
    p_iter = iter(p)


    assert id(p) != id(p_iter),  "Not an iterable!"
    assert id(p_iter) == id(iter(p_iter)), "Not an iterator"

    _ = [next(p_iter) for i in range(n-2)] 

    with pytest.raises(StopIteration):
        next(p_iter)

    assert '__iter__' in dir(p_iter), "iterator not found"
    assert '__next__' in dir(p_iter), "iterator needs __next__"

    assert '__iter__' in dir(p), "iterable needs __iter__"
    assert '__next__' not in dir(p), "iterable doesn't need __next__"


Colab Notebook

epai_session11's People

Contributors

hemanthreddy-k avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.