This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathover.py
73 lines (62 loc) · 1.65 KB
/
over.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Using match to simplify the implementation of overloaded functions.
#
# PEP 484 overloaded functions must be followed by a single implementation.
# Those implementations traditionally use `isinstance()` and other hacks.
# Using `match` we can make the implementation much cleaner.
from dataclasses import dataclass
from typing import overload
# First example: overload add() to support scalars and lists.
@overload
def add(a: int, b: int) -> int:
pass
@overload
def add(a: list[int], b: list[int]) -> list[int]:
pass
def add(a, b):
match a, b:
case list(), list():
return [ai + bi for ai, bi in zip(a, b, strict=True)]
case int(), int():
return a + b
case _:
raise TypeError("incompatible arguments")
print(add(1, 2))
print(add([1, 2], [3, 4]))
# Second example: create a 3D point from various inputs.
@dataclass
class Point2d:
x: int
y: int
@dataclass
class Point3d:
x: int
y: int
z: int
@overload
def point(p: Point2d) -> Point3d:
pass
@overload
def point(p: Point3d) -> Point3d:
pass
@overload
def point(x: int, y: int) -> Point3d:
pass
@overload
def point(x: int, y: int, z: int) -> Point3d:
pass
def point(*args):
match args:
case [Point2d(x, y)]:
return Point3d(x, y, 0)
case [p := Point3d()]:
return p
case [x := int(), y := int()]:
return Point3d(x, y, 0)
case [x := int(), y := int(), z := int()]:
return Point3d(x, y, z)
case _:
raise TypeError("Huh?")
print(point(1, 2))
print(point(1, 2, 3))
print(point(Point2d(1, 2)))
print(point(Point3d(1, 2, 3)))