Python 3 个继承属性未找到
Python 3 inheritance attribute not found
我正在通过一个项目学习 Python,并且 运行 遇到了 subclassing 的问题。根据我所阅读的所有内容,该过程非常简单,但是从子 class 调用基础 class 的方法会导致属性未找到错误。
我的代码示例包括基础 class(玩家)、子class(花名册)的相关部分以及对基础和子 classes 的设置和调用.我错过了什么?如果真的不需要,是否有必要在 subclass 中创建一个 init 方法?
主脚本:
from battleship.player import Player, Roster
player=Player()
roster=Roster()
player.register()
roster.load_other_players()
类:
class Player:
def __init__(self):
....
self.getOtherPlayers = {}
def register(self):
...
return
def get_other_players (self):
return self.getOtherPlayers
def gameFlow(self):
if self.currentFlow != undefined:
self.currentFlow+=1
else:
self.currentFlow = 0
def setMove(self, m):
return move.setMove(m);
class Roster(Player):
def __init__(self):
super().__init__()
def load_other_players(self):
print("Roster!!!")
print("Other players: ", super.get_other_players())
Return:
Roster!!!
Traceback (most recent call last):
File "player.py", line 23, in <module>
roster.load_other_players()
File "/home/jspooner/projects/battleship/BotPlayer/battleship/player.py", line 152, in load_other_players
print("Other players: ", super.get_other_players())
AttributeError: type object 'super' has no attribute 'get_other_players'
编辑
如果以后有人提到这个问题,我的实际问题是一个设计问题,我创建了一个基础 class 的实例以及一个子 class 的实例。结果,我有 2 个相关的 class 实例,它们彼此没有关联。
为了解决这个问题,我交换了层次结构,这样 Roster 就是父级 class 而 Player 就是子级。我的 main 创建了一个 Player 实例,它自然创建了一个相关的 Roster class.
这个很简单,你忘了super里面的()。您应该这样调用该函数:super().get_other_players()
而不是 super.get_other_players()
,因为 super 是一个函数。
此外,如果您不需要的话,在您的子类中使用构造函数(在子类中定义一个 __init__()
方法)绝对不是必要的。
要解决您的主要 question/issues:
super
不是有效实体,它应该是函数 super()
。或者,由于 Roster
继承自 Player
,因此 self.get_other_players()
也是一种可接受的引用方式,因为所有超类的属性都被合并为子类。
__init__
不需要显式创建,但如果你想执行一些初始化,它是必需的。值得阅读 THIS 答案以获得更多 in-depth 信息,了解 __init__
实际发生的事情以及为什么需要这样做。
回答一个你没有问过的问题:Python 在某些方面与其他一些语言不同,最显着的是不需要使用 getter 和 setter。而不是 super().get_other_players()
或等价物,只需直接访问 getOtherPlayers
属性。它更优雅,代码行更少,并且在维护代码时更容易 read/debug(更多 'pythonic')
我正在通过一个项目学习 Python,并且 运行 遇到了 subclassing 的问题。根据我所阅读的所有内容,该过程非常简单,但是从子 class 调用基础 class 的方法会导致属性未找到错误。
我的代码示例包括基础 class(玩家)、子class(花名册)的相关部分以及对基础和子 classes 的设置和调用.我错过了什么?如果真的不需要,是否有必要在 subclass 中创建一个 init 方法?
主脚本:
from battleship.player import Player, Roster
player=Player()
roster=Roster()
player.register()
roster.load_other_players()
类:
class Player:
def __init__(self):
....
self.getOtherPlayers = {}
def register(self):
...
return
def get_other_players (self):
return self.getOtherPlayers
def gameFlow(self):
if self.currentFlow != undefined:
self.currentFlow+=1
else:
self.currentFlow = 0
def setMove(self, m):
return move.setMove(m);
class Roster(Player):
def __init__(self):
super().__init__()
def load_other_players(self):
print("Roster!!!")
print("Other players: ", super.get_other_players())
Return:
Roster!!!
Traceback (most recent call last):
File "player.py", line 23, in <module>
roster.load_other_players()
File "/home/jspooner/projects/battleship/BotPlayer/battleship/player.py", line 152, in load_other_players
print("Other players: ", super.get_other_players())
AttributeError: type object 'super' has no attribute 'get_other_players'
编辑 如果以后有人提到这个问题,我的实际问题是一个设计问题,我创建了一个基础 class 的实例以及一个子 class 的实例。结果,我有 2 个相关的 class 实例,它们彼此没有关联。
为了解决这个问题,我交换了层次结构,这样 Roster 就是父级 class 而 Player 就是子级。我的 main 创建了一个 Player 实例,它自然创建了一个相关的 Roster class.
这个很简单,你忘了super里面的()。您应该这样调用该函数:super().get_other_players()
而不是 super.get_other_players()
,因为 super 是一个函数。
此外,如果您不需要的话,在您的子类中使用构造函数(在子类中定义一个 __init__()
方法)绝对不是必要的。
要解决您的主要 question/issues:
super
不是有效实体,它应该是函数 super()
。或者,由于 Roster
继承自 Player
,因此 self.get_other_players()
也是一种可接受的引用方式,因为所有超类的属性都被合并为子类。
__init__
不需要显式创建,但如果你想执行一些初始化,它是必需的。值得阅读 THIS 答案以获得更多 in-depth 信息,了解 __init__
实际发生的事情以及为什么需要这样做。
回答一个你没有问过的问题:Python 在某些方面与其他一些语言不同,最显着的是不需要使用 getter 和 setter。而不是 super().get_other_players()
或等价物,只需直接访问 getOtherPlayers
属性。它更优雅,代码行更少,并且在维护代码时更容易 read/debug(更多 'pythonic')