1

I am a beginner trying to perform inheritance using Python. So I decided to practice the example programs from the book "Data Structures and Algorithms in Python" by Michael T. Goodrich.

Here is the code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


class Progression:
    def __init__(self,start=0):
        self._current = start

    def _advanced(self):
        self._current += 1

    def __next__(self):
        if self._current is None:
            raise StopIteration()
        else:
            answer = self._current
            self._advanced()
            return answer

    def __iter__(self):
        return self

    def print_progression(self,n):
        print(' '.join(str(next(self)) for j in range(n)))


class ArithmeticProgression:
    def __init__(self,increment=1,start=0):
        super().__init__(start)
        self._increment = increment

    def _advance(self):
        self._current += self._increment

class GeometricProgression(Progression):

    def __init__(self,base=2,start=1):
        super().__init__(start)
        self._base = base

    def _advance(self):
        self._current *= self._base

class FibonacciProgression(Progression):
    def __init__(self,first=0,second=1):
        super().__init__(first)
        self._prev = second - first

    def _advance(self):
        self._prev, self._current = self._current,self._prev + self._current

if __name__ == '__main__':
    print('Default Progression: ')
    Progression().print_progression(10)

    print('Arithmetic progression with increment 5 and start 2:')
    ArithmeticProgression(5,2).print_progression(10)

    print('Geometric progression with default base:')
    GeometricProgression().print_progression(10)

    print('Geometric progression with increasing it to the power of 2')
    GeometricProgression(3).print_progression(10)

    print('Fibonacci progression with default start progression')
    FibonacciProgression().print_progression(10)

    print('Fibonacci progression with default start progression')
    FibonacciProgression(4,6).print_progression(10)

Here is the error:

Default Progression: 
0 1 2 3 4 5 6 7 8 9
Arithmetic progression with increment 5 and start 2:
Traceback (most recent call last):

  File "some location", line 61, in <module>
    ArithmeticProgression(5,2).print_progression(10)

  File "some location", line 33, in __init__
    super().__init__(start)

TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Any help will be appreciated. Here I am trying to check the super().init(start) of ArithmeticProgression but am getting very confused with the passing of elements in init() example. Any help will be appreciated. Also I am a beginner.

1 Answer 1

1

ArithmeticProgression does not inherit from Progression like GeometricProgression does. So there's no base class to call with super().

Replace class ArithmeticProgression(Progression): with class ArithmeticProgression:

To make it short: you just forgot (Progression)

Sign up to request clarification or add additional context in comments.

1 Comment

To add to this, there's also a typo in his _advance method in Progression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.