Giter Site home page Giter Site logo

magic-in-ten-mins-py's Issues

1 里面可以用 dataclass 简化代码

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())))))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.