假设我有以下两门课:
class Literal:
pass
class Expr:
pass
class MyClass:
def __init__(self, Type:OneOf(Literal, Expr)):
pass如何使类型成为Expr或Literal类之一?我想要做的事情的全部例子如下:
from enum import Enum
PrimitiveType = Enum('PrimitiveType', ['STRING', 'NUMBER'])
class Array:
def __init__(self, Type:OneOf[Array,Struct,PrimitiveType]):
self.type = Type
class Pair:
def __init__(self, Key:str, Type:OneOf[Array,Struct,PrimitiveType]):
self.Key = Key
self.Type = Type
class Struct:
def __init__(self, tuple[Pair])发布于 2022-11-13 01:14:04
您需要一个Union类型:
from typing import Union
def __init__(self, Key: str, Type: Union[Array, Struct, PrimitiveType]):
# or in 3.10+
def __init__(self, Key: str, Type: Array | Struct | PrimitiveType):发布于 2022-11-13 01:19:23
from typing import Union和
def __init__(self, Type:Union[Array,Struct,PrimitiveType])https://stackoverflow.com/questions/74417729
复制相似问题