Issue
Let's say I have the following two classes:
class Literal:
pass
class Expr:
pass
class MyClass:
def __init__(self, Type:OneOf(Literal, Expr)):
pass
How would I make the type one of the Expr
or Literal
class? The full example of what I'm trying to do is as follows:
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])
Solution
You want a Union
type:
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):
Answered By - Carcigenicate
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.