-
Notifications
You must be signed in to change notification settings - Fork 3
/
PythonDataclass.py
executable file
·54 lines (40 loc) · 1.1 KB
/
PythonDataclass.py
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
#! /usr/bin/env python
from dataclasses import dataclass, FrozenInstanceError
def example_frozen_dataclass():
"""Implement example of frozen dataclass similar to class factory."""
@dataclass(frozen=True)
class Author:
"""Define Author type."""
name: str
desc: str
author = Author(
name='VictorB',
desc='Engineer'
)
print(author.name)
print(author.desc)
try:
author.desc = 'Scientist'
except FrozenInstanceError:
print('You cannot set this attribute (read-only).')
def example_unfrozen_dataclass():
"""Implement example of frozen dataclass similar to class factory."""
@dataclass
class Author:
"""Define Author type."""
name: str
desc: str
author = Author(
name='VictorB',
desc='Engineer'
)
print(author.name)
print(author.desc)
try:
author.desc = 'Scientist'
except FrozenInstanceError:
print('You cannot set this attribute (read-only).')
print(author.desc)
example_frozen_dataclass()
print()
example_unfrozen_dataclass()