View Code? Open in Web Editor
NEW
十分钟魔法练习 Python 版
License: Creative Commons Attribution 4.0 International
magic-in-ten-mins-py's Introduction
-
🤔 I'm a Software Engineer.
-
🔭 Working on tencent, focus on PL implementation, interested in PL/Security related techniques.
magic-in-ten-mins-py's People
Contributors
magic-in-ten-mins-py's Issues
py 的枚举类比较弱。所以需要用 Union 或者 | 来表达和类型。(利用继承的思路学到了)
from __future__ import annotations
import enum
from dataclasses import dataclass
from typing import Union
@dataclass
class Student:
name: str
id: int
@dataclass
class Teacher:
name: str
office: str
SchoolPerson = Union[Student, Teacher]
class Bool(enum.Enum):
TRUE = enum.auto()
FALSE = enum.auto()
def test_bool():
b = Bool.FALSE
match b:
case Bool.TRUE:
...
case Bool.FALSE:
print("oh my?")
test_bool()
@dataclass
class O:
...
@dataclass
class S:
x: Nat
def __repr__(self) -> str:
def count(x: Nat):
match x:
case S(x):
return 1 + count(x)
case O():
return 0
return str(count(self))
Nat = S | O
print(S(S(S(S(O())))))