python - Python variable length generics types
问题描述
Is is possible to create a generic type that access a variable length argument types?
Basically, I'm trying to create a generic observable where the user can define which arguments they want to accept with type hints.
Ex:
Types = TypeVar("Types", var_length=True)
class Obserable(Generic[Types]):
def subscribe(func: Callable[[Types], None]):
...
def notify(*args: Types):
...
def callback(arg1: int, arg2: str, arg3: int) -> None:
...
observer: Observable[int, str, int] = Observable()
observer.subscribe(callback)
observer.notify(1, "hello", 5)
解决方案
推荐阅读
- php - Symfony 4.3.2 和 FOS 用户捆绑控制器的注入依赖性问题
- c++ - 从成员双端队列访问父母数据的方法
- c# - ProActive bot 向用户发送消息的频率如何?
- laravel - 配置 Apache 反向代理以在生产环境中托管 Laravel Echo 服务器
- android - 无法在所有设备中安装“应用程序名称”错误。不适用于特定用户
- ios - Segue 调用了两次 - IOS
- visual-studio-app-center - lfs 使用自定义 lfs 服务器 App Center
- c++ - C++ boost::bimap insert() 方法不起作用
- hibernate - Hibernate 一对多关系查询问题
- angular - 使用 OnPush 策略对子元素进行 ChangeDetection