Skip to content

Commit 7c90f8a

Browse files
committed
feat(python): add notes about weird behaviour of data classes
1 parent 531ad7c commit 7c90f8a

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

content/python/general.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
# General Python Stuff
22

3+
## Weird data classes
4+
5+
This will work, as it recognises them as parameters when you type hint:
6+
7+
```
8+
@dataclass
9+
class ASUArrivals:
10+
stroke: float = 1.2
11+
tia: float = 9.3
12+
neuro: float = 3.6
13+
other: float = 3.2
14+
15+
ASUArrivals(stroke=4)
16+
```
17+
18+
But without, it recognises them as class attributes, and so won't work:
19+
20+
```
21+
@dataclass
22+
class ASUArrivals:
23+
stroke = 1.2
24+
tia = 9.3
25+
neuro = 3.6
26+
other = 3.2
27+
28+
ASUArrivals(stroke=4)
29+
30+
--> TypeError: ASUArrivals.__init__() got an unexpected keyword argument 'stroke'
31+
```
32+
33+
It doesn't do any validation with the type hints - you can say int, but it doesn't care if you use int, for example.
34+
35+
```
36+
@dataclass
37+
class ASUArrivals:
38+
stroke: int = 1.2
39+
tia: float = 9.3
40+
neuro: float = 3.6
41+
other: float = 3.2
42+
43+
ASUArrivals(stroke=4.3)
44+
```
45+
46+
If you miss type hints for any of them, they just get dropped from the class entirely
47+
48+
```
49+
@dataclass
50+
class ASUArrivals:
51+
stroke: float = 1.2
52+
tia = 9.3
53+
neuro: float = 3.6
54+
other: float = 3.2
55+
56+
ASUArrivals()
57+
--> ASUArrivals(stroke=1.2, neuro=3.6, other=3.2)
58+
```
59+
360
## Jupyter notebook merge conflicts
461

562
```

0 commit comments

Comments
 (0)