【发布时间】:2020-07-27 22:28:31
【问题描述】:
我假设这个问题已经被问了一百万次了,但我似乎无法理解一些事情,所以请在这里耐心等待。我正在尝试在 Python 中进行通用继承。这就是我想要完成的:我有一个接受泛型类型的方法,它返回一个从该父类继承的类
这就是代码的样子
def make_foo(parent):
class Relationship(parent):
# def __init__(self):
@staticmethod
def should_see_this_method(self):
print("Hello here")
return True
return Relationship
现在这是我拥有的一段代码
NewType = make_relationship(str)
another_test: NewType = "Hello"
another_test.should_see_this_method()
another_test.capitalize()
现在我收到AttributeError: 'str' object has no attribute 'should_see_this_method'
我不确定这是否是反模式,但我只是好奇知道如何做到这一点。
谢谢
【问题讨论】:
标签: python