Python 3.6:我想念一些泛型打字超链接继承
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python 3.6:我想念一些泛型打字超链接继承,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3127字,纯文字阅读大概需要5分钟。
内容图文
![Python 3.6:我想念一些泛型打字超链接继承](/upload/InfoBanner/zyjiaocheng/728/be263c0bb0f54a519099c6006ff3e6e0.jpg)
首先,我运行以下代码,这真的很好:
class Monster:
def __init__(self):
self._can_do = []
print("created a monster")
super().__init__()
class Race(Monster):
""" all races must derive from this """
def __init__(self):
super().__init__()
print("created a race x")
class Human(Race):
def __init__(self):
super().__init__()
self._can_do.append("Do nothing special !")
print("created a human")
class Elf(Race):
def __init__(self):
super().__init__()
self._can_do.append("Avoid sleep")
print("created an elf")
class Class:
""" all classes must derive from this """
def __init__(self):
super().__init__()
print("created a class x")
class Fighter(Class):
def __init__(self):
super().__init__()
self._can_do.append("Hit hard")
print("created a fighter")
class Wizard(Class):
def __init__(self):
super().__init__()
self._can_do.append("Cast spells")
print("created a wizard")
class Hero(Human, Fighter):
def __init__(self):
x = super()
print(f"super = {x}")
super().__init__()
def speak(self):
for action in self._can_do:
print(f"I can {action} !")
print("creating hero 1 :")
hero1 = Hero()
print("hero 1 human fighter says :")
hero1.speak()
结果是:
creating hero 1 :
created a monster
created a class x
created a fighter
created a race x
created a human
hero 1 human fighter says :
I can Hit hard !
I can Do nothing special ! !
然后我又去了,稍微改变一下代码,如下所示,因为那是我想要去的:
(使Hero类动态而不是静态地进行heriting)
import typing
class Monster:
def __init__(self):
self._can_do = []
print("created a monster")
super().__init__()
class Race(Monster):
""" all races must derive from this """
def __init__(self):
super().__init__()
print("created a race x")
class Human(Race):
def __init__(self):
super().__init__()
self._can_do.append("Do nothing special !")
print("created a human")
class Elf(Race):
def __init__(self):
super().__init__()
self._can_do.append("Avoid sleep")
print("created an elf")
class Class:
""" all classes must derive from this """
def __init__(self):
super().__init__()
print("created a class x")
class Fighter(Class):
def __init__(self):
super().__init__()
self._can_do.append("Hit hard")
print("created a fighter")
class Wizard(Class):
def __init__(self):
super().__init__()
self._can_do.append("Cast spells")
print("created a wizard")
RaceT = typing.TypeVar('RaceT', bound=Race)
ClassT = typing.TypeVar('ClassT', bound=Class)
class Hero(typing.Generic[RaceT, ClassT]):
def __init__(self):
super().__init__()
def speak(self):
for action in self._can_do:
print(f"I can {action} !")
print("creating hero 1 :")
hero1 = Hero[Human,Fighter]()
print("hero 1 human fighter says :")
hero1.speak()
这一次,都出错了:
creating hero 1 :
hero 1 human fighter says :
Traceback (most recent call last):
File "./test2.py", line 61, in <module>
hero1.speak()
File "./test2.py", line 54, in speak
for action in self._can_do:
AttributeError: 'Hero' object has no attribute '_can_do'
>似乎使用泛型类创建使超级无法找到父类初始值设定项,非?
>我错过了什么吗?
解决方法:
这不是泛型打字的意思.当你申报时
class Hero(typing.Generic[RaceT, ClassT]):
...
那意味着Hero需要两个类型参数.这并不意味着Hero [Human,Fighter]是Human或Fighter的子类,不仅仅是List [int]是int的子类.通用类型不是动态调整类的超类的方法.
内容总结
以上是互联网集市为您收集整理的Python 3.6:我想念一些泛型打字超链接继承全部内容,希望文章能够帮你解决Python 3.6:我想念一些泛型打字超链接继承所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。