使用继承时 __init__() 中的参数数量问题
Issue with number of arguments in __init__() while using Inheritance
我是一个尝试使用 Python 执行继承的初学者。所以我决定练习 Michael T. Goodrich "Data Structures and Algorithms in Python" 一书中的示例程序。
代码如下:
#!/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)
这里是错误:
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)
如有任何帮助,我们将不胜感激。在这里,我试图检查 ArithmeticProgression 的 super().init(start) 但我对 init( ) 例子。任何帮助将不胜感激。我也是初学者
ArithmeticProgression
不像 GeometricProgression
那样继承自 Progression。所以没有基础 class 可以调用 super()
.
替换
class ArithmeticProgression(Progression):
和
class ArithmeticProgression:
简而言之:你只是忘记了(Progression)
我是一个尝试使用 Python 执行继承的初学者。所以我决定练习 Michael T. Goodrich "Data Structures and Algorithms in Python" 一书中的示例程序。
代码如下:
#!/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)
这里是错误:
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)
如有任何帮助,我们将不胜感激。在这里,我试图检查 ArithmeticProgression 的 super().init(start) 但我对 init( ) 例子。任何帮助将不胜感激。我也是初学者
ArithmeticProgression
不像 GeometricProgression
那样继承自 Progression。所以没有基础 class 可以调用 super()
.
替换
class ArithmeticProgression(Progression):
和
class ArithmeticProgression:
简而言之:你只是忘记了(Progression)