python - Python - 如何在使用泛型类型时访问其他类方法?
问题描述
我想要一个类方法返回正确的类型,所以我为它声明泛型类型。的任何子类TrivialClass
也将满足类型提示。但是在from_int()
,内部cls
将无法访问other()
Pycharm 中的方法。正确的方法是什么?
from typing import Type, TypeVar
T = TypeVar('T', bound='TrivialClass')
class TrivialClass:
# ...
@classmethod
def from_int(cls: Type[T], int_arg: int) -> T:
# no suggestion for other, since cls type is Type[T]
cls.other()
return cls(...)
@classmethod
def other(cls):
pass
解决方案
推荐阅读
- javascript - 如何在不重复的情况下随机匹配同一个数组中的几个数据?
- java - Itemtouchhelper & ActionMode:如何组合多选和重新排列项目
- python - 从 python 导出格式良好的文本到 word doc
- elasticsearch - 匹配数组中只有一个字母的精确字符串
- python - Tkinter 全局变量/.get()
- javascript - 有没有办法通过firebase google auth获取用户位置信息?
- mongodb - MongoDB对多个字段的聚合查询
- pandas - 如何在主列下创建子列
- python - 带有参数的 Pyodbc SQL Server 存储过程在本地机器上工作,但在服务器上不工作
- scala - 有一个旧库没有为 scala 2.13.x 构建,如何创建本地 maven 存储库查找?