dataclasses ¶
Functions:
-
asdict
–Return the fields of a dataclass instance as a new dictionary mapping
-
astuple
–Return the fields of a dataclass instance as a new tuple of field values.
-
dataclass
–Returns the same class as was passed in, with dunder methods
-
field
–Return an object to identify dataclass fields.
-
fields
–Return a tuple describing the fields of this dataclass.
-
is_dataclass
–Returns True if obj is a dataclass or an instance of a
-
make_dataclass
–Return a new dynamically created dataclass.
-
replace
–Return a new object replacing specified fields with new values.
asdict ¶
asdict(obj, *, dict_factory=dict)
Return the fields of a dataclass instance as a new dictionary mapping field names to field values.
Example usage:
@dataclass class C: x: int y: int
c = C(1, 2) assert asdict(c) == {'x': 1, 'y': 2}
If given, 'dict_factory' will be used instead of built-in dict. The function applies recursively to field values that are dataclass instances. This will also look into built-in containers: tuples, lists, and dicts.
Source code in src/sake/dataclasses.py
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 |
|
astuple ¶
astuple(obj, *, tuple_factory=tuple)
Return the fields of a dataclass instance as a new tuple of field values.
Example usage::
@dataclass class C: x: int y: int
c = C(1, 2) assert astuple(c) == (1, 2)
If given, 'tuple_factory' will be used instead of built-in tuple. The function applies recursively to field values that are dataclass instances. This will also look into built-in containers: tuples, lists, and dicts.
Source code in src/sake/dataclasses.py
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 |
|
dataclass ¶
dataclass(
cls=None,
/,
*,
init=True,
repr=True,
eq=True,
order=False,
unsafe_hash=False,
frozen=False,
match_args=True,
kw_only=False,
slots=False,
)
Returns the same class as was passed in, with dunder methods added based on the fields defined in the class.
Examines PEP 526 annotations to determine fields.
If init is true, an init() method is added to the class. If repr is true, a repr() method is added. If order is true, rich comparison dunder methods are added. If unsafe_hash is true, a hash() method function is added. If frozen is true, fields may not be assigned to after instance creation. If match_args is true, the match_args tuple is added. If kw_only is true, then by default all fields are keyword-only. If slots is true, an slots attribute is added.
Source code in src/sake/dataclasses.py
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 |
|
field ¶
field(
*,
default=MISSING,
default_factory=MISSING,
init=True,
repr=True,
hash=None,
compare=True,
metadata=None,
kw_only=MISSING
)
Return an object to identify dataclass fields.
default is the default value of the field. default_factory is a 0-argument function called to initialize a field's value. If init is true, the field will be a parameter to the class's init() function. If repr is true, the field will be included in the object's repr(). If hash is true, the field will be included in the object's hash(). If compare is true, the field will be used in comparison functions. metadata, if specified, must be a mapping which is stored but not otherwise examined by dataclass. If kw_only is true, the field will become a keyword-only parameter to init().
It is an error to specify both default and default_factory.
Source code in src/sake/dataclasses.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
fields ¶
fields(class_or_instance)
Return a tuple describing the fields of this dataclass.
Accepts a dataclass or an instance of one. Tuple elements are of type Field.
Source code in src/sake/dataclasses.py
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 |
|
is_dataclass ¶
is_dataclass(obj)
Returns True if obj is a dataclass or an instance of a dataclass.
Source code in src/sake/dataclasses.py
1238 1239 1240 1241 1242 |
|
make_dataclass ¶
make_dataclass(
cls_name,
fields,
*,
bases=(),
namespace=None,
init=True,
repr=True,
eq=True,
order=False,
unsafe_hash=False,
frozen=False,
match_args=True,
kw_only=False,
slots=False
)
Return a new dynamically created dataclass.
The dataclass name will be 'cls_name'. 'fields' is an iterable of either (name), (name, type) or (name, type, Field) objects. If type is omitted, use the string 'typing.Any'. Field objects are created by the equivalent of calling 'field(name, type [, Field-info])'.
C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))
is equivalent to:
@dataclass class C(Base): x: 'typing.Any' y: int z: int = field(init=False)
For the bases and namespace parameters, see the builtin type() function.
The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to dataclass().
Source code in src/sake/dataclasses.py
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 |
|
replace ¶
replace(obj, /, **changes)
Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@dataclass(frozen=True) class C: x: int y: int
c = C(1, 2) c1 = replace(c, x=3) assert c1.x == 3 and c1.y == 2
Source code in src/sake/dataclasses.py
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 |
|