r/shittyprogramming • • Feb 02 '24

I implemented Python's missing length function

Post image
565 Upvotes

25 comments sorted by

View all comments

33

u/Gravbar Feb 02 '24

TIL py finally added switch statements

0

u/[deleted] Feb 02 '24

[deleted]

7

u/frivol Feb 02 '24

Probably took that long to decide on a fall-through policy.

3

u/rubikssolver4 Feb 03 '24

Two years ago is nothing

7

u/itsTyrion Feb 04 '24

It’s not just basic Switch-Case, it’s pattern matching

5

u/AlarmDozer Feb 03 '24

Wait, for real? I gotta test this.

4

u/erosPhoenix Feb 04 '24

They're pretty sweet too. Incredibly expressive.

You can even use them to finally destructure objects and dicts, although it's a little hacky:

d = {'a': 1, 'b': 2, 'c': 3}
match d:
	case { 'a': 1, 'b': b, **rest }:
		pass
print(b, rest) # prints 2, {"c": 3}

from dataclasses import dataclass
@dataclass
class Point:
	x: int
	y: int
	z: int
	
p = Point(4, 5, 6)
match p:
	case Point(4, y, z):
		pass
print(y, z) # prints 5, 6

1

u/snaredr Feb 02 '24

came out two years ago and the first usage you've seen of it is comedically awful way to write a len function. You and me both lmao