Python iterator as class member?

Jiangfan Du

I have a scheme like:

class Base(object):
    param, d=0, 1
    def get_all(self):
        while True:
            a = self.get_xxx(param)
            if not a:
                break
            handle(a)
            param += d

class A(Base):
    def get_xxx(param):
        return some_method(param)

class B(Base):
    def get_xxx(param):
        return other_method(param)

Then, I was informed that for B, after each get_xxx param should be a+1 instead of param+d. That means I need to extract the param change logic at the end of get_all. I came up with a scheme using iterator:

class Base(object):
    def get_all(self):
        get_xxx = self.get_xxx()
        while True:
            a = get_xxx.next()
            if not a:
                break
            handle(a)

class A(Base):
    def get_xxx():
        param, d = 0, 1
        while True:
            yield somemethod(param)
            param += d

class B(Base):
    def get_xxx():
        param = 0
        while True:
            a = somemethod(param)
            param = a + 1
            yield a

Problem solved, but somehow I feel uncomfortable. So I wonder if there's a better solution? Many thanks!

jonrsharpe

I would make things like param and d instance attributes:

class Base(object):
    def __init__(self):
        self.param = 0
        self.d = 1

then you don't have to pass anything explicitly into get_xxx(). You could replace

param += d

with

self.iterate_param(a):

in Base.get_all() then define iterate_param() appropriately in your two subclasses, i.e.

class A(Base):
    ...
    def iterate_param(self, a):
        self.param += self.d

class B(Base):
    ...
    def iterate_param(self, a):
        self.param = a + 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

std::vector iterator as a class member

From Dev

Python class iterator

From Dev

C++: Forward iterator interface of a member container to class interface

From Dev

iterator_category': is not a member of any base class of 'std::iterator_traits<_InIt>'

From Dev

Python: Hide member of base class in derived class

From Dev

Duplicate each member in an iterator

From Dev

Python classmethods: what's the difference between a member of an instance and a member of the class?

From Dev

Python: appending to a base class list/tuple member

From Dev

How the Dict work in Python? Is it a static member in class?

From Dev

Adding a member variable to inherited class in Python

From Dev

unable to call member function in class python

From Dev

How to make class member process safe in Python

From Dev

How to define a member function outside class in python?

From Dev

Member of class point to member of class

From Dev

Iterator with Static Array Member variable

From Dev

Python defining an iterator class, failed with "iter() returned non-iterator of type 'Fib'"

From Dev

Iterator python

From Dev

Iterator python

From Dev

Iterator.class vs Iterator<String>.class

From Dev

Iterator.class vs Iterator<String>.class

From Dev

Class has no member "Class"

From Dev

Anonymous class as member of class

From Dev

Selectively hide member of member of class

From Dev

Python How to override a class member in the child and access it from parent?

From Dev

python class static method dynamic binding static data member

From Dev

Boost.Python: expose class member which is a pointer

From Dev

How to add a static member to a Cython class (from python, not C)

From Dev

Overloading the [] operator in python class to refer to a numpy.array data member

From Dev

Python How to override a class member in the child and access it from parent?

Related Related

  1. 1

    std::vector iterator as a class member

  2. 2

    Python class iterator

  3. 3

    C++: Forward iterator interface of a member container to class interface

  4. 4

    iterator_category': is not a member of any base class of 'std::iterator_traits<_InIt>'

  5. 5

    Python: Hide member of base class in derived class

  6. 6

    Duplicate each member in an iterator

  7. 7

    Python classmethods: what's the difference between a member of an instance and a member of the class?

  8. 8

    Python: appending to a base class list/tuple member

  9. 9

    How the Dict work in Python? Is it a static member in class?

  10. 10

    Adding a member variable to inherited class in Python

  11. 11

    unable to call member function in class python

  12. 12

    How to make class member process safe in Python

  13. 13

    How to define a member function outside class in python?

  14. 14

    Member of class point to member of class

  15. 15

    Iterator with Static Array Member variable

  16. 16

    Python defining an iterator class, failed with "iter() returned non-iterator of type 'Fib'"

  17. 17

    Iterator python

  18. 18

    Iterator python

  19. 19

    Iterator.class vs Iterator<String>.class

  20. 20

    Iterator.class vs Iterator<String>.class

  21. 21

    Class has no member "Class"

  22. 22

    Anonymous class as member of class

  23. 23

    Selectively hide member of member of class

  24. 24

    Python How to override a class member in the child and access it from parent?

  25. 25

    python class static method dynamic binding static data member

  26. 26

    Boost.Python: expose class member which is a pointer

  27. 27

    How to add a static member to a Cython class (from python, not C)

  28. 28

    Overloading the [] operator in python class to refer to a numpy.array data member

  29. 29

    Python How to override a class member in the child and access it from parent?

HotTag

Archive