Is this a bug in Python inheritance?
本问题已经有最佳答案,请猛点这里访问。
我不确定此代码的输出是否正确或是否存在错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class F: """An abstract class""" list_of_secrets = [] def __init__(self): pass def getSecret(self): return self.list_of_secrets class F_None(F): pass class F_Some(F): def __init__(self): self.list_of_secrets.append("secret value!") x = F_Some() print"x:",x.getSecret() y = F_None() print"y:",y.getSecret() |
使用python 2.7.3的输出:
我认为它应该输出:
有什么想法吗?
1 2 | def __init__(self): self.list_of_secrets = [] |
你从不定义
1 2 3 4 5 6 7 | class F: """An abstract class""" def __init__(self): self.list_of_secrets = [] def getSecret(self): return self.list_of_secrets |