-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathdataclass_example.py
More file actions
executable file
·100 lines (77 loc) · 2.4 KB
/
dataclass_example.py
File metadata and controls
executable file
·100 lines (77 loc) · 2.4 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
"""
This file is part of eRCaGuy_hello_world:
https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
GS
Jan. 2026
Practice using dataclasses as C-style structs in Python!
See my answer: https://stackoverflow.com/a/77161026/4561887
Status: Done and works!
keywords: (keywords)
Check this script with `pylint` v2.0.0 or later. See "eRCaGuy_hello_world/python/README.md" for
installation instructions to install the latest version from GitHub.
For a list of all error codes, such as `C0301`, `C0116`, `W0105`, etc., see here:
https://pylint.pycqa.org/en/latest/messages/messages_list.html
```bash
pylint dataclass_example.py
```
Run command:
```bash
./dataclass_example.py
# OR
python3 dataclass_example.py
```
References:
1. My answer: https://stackoverflow.com/a/77161026/4561887
1. Official Python documentation on dataclasses: https://docs.python.org/3/library/dataclasses.html
1. eRCaGuy_hello_world/python/struct_c_like.py
"""
from dataclasses import dataclass
@dataclass
class MyStruct:
"""
A simple struct-like dataclass with some fields of various types.
2 fields have default values.
"""
name: str
x: int
y: int
z: float = 0.0 # default value
is_ready: bool = False # default value
# Create an instance of MyStruct
my_data = MyStruct(name="My struct", x=123, y=456)
my_data.z = 789.001 # set z later if desired
my_data.x = 999 # modify x later if desired
my_data2 = MyStruct(
name="Another struct",
x=10,
y=20,
z=30.0,
is_ready=True
)
print(my_data)
print(my_data2)
# ---------------------
# Illegal operations:
# ---------------------
# TypeError: MyStruct.__init__() missing 1 required positional argument: 'y'
# my_data = MyStruct(name="Some useful name of this data", x=123)
# ---------------------
# Adding new fields on the fly
# - kind of works, but is not recommended
# ---------------------
# Adding a new member `w` at run-time is allowed, but not recommended
my_data.w = 7
# will NOT print the newly-added `w` member; prints only:
# `MyStruct(name='My struct', x=123, y=456, z=789.001, is_ready=False)`
print(my_data)
# will print `7`
print(my_data.w)
# pylint: disable-next=pointless-string-statement
"""
SAMPLE OUTPUT:
MyStruct(name='My struct', x=999, y=456, z=789.001, is_ready=False)
MyStruct(name='Another struct', x=10, y=20, z=30.0, is_ready=True)
MyStruct(name='My struct', x=999, y=456, z=789.001, is_ready=False)
7
"""