質問編集履歴
1
頂いたご意見を踏まえて書き直してみました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -109,3 +109,57 @@
|
|
109
109
|
|
110
110
|
|
111
111
|
よろしくお願いします。
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
追記(2021-11-22)
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
from dataclasses import dataclass, field
|
126
|
+
|
127
|
+
from typing import Dict
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
@dataclass(frozen=True)
|
132
|
+
|
133
|
+
class Config:
|
134
|
+
|
135
|
+
label_a: int = 1
|
136
|
+
|
137
|
+
label_b: int = 0
|
138
|
+
|
139
|
+
label_c: int = -1
|
140
|
+
|
141
|
+
_labels: Dict[int, str] = field(init=False)
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
def __post_init__(self):
|
146
|
+
|
147
|
+
object.__setattr__(self, '_labels', {v: k for k, v in vars(self).items()})
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def get_label(self, val):
|
152
|
+
|
153
|
+
return self._labels.get(val)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
config = Config()
|
158
|
+
|
159
|
+
print(config.label_a) # 1
|
160
|
+
|
161
|
+
print(config._labels) # {1: 'label_a', 0: 'label_b', -1: 'label_c'}
|
162
|
+
|
163
|
+
print(config.get_label(-1)) # label_c
|
164
|
+
|
165
|
+
```
|