■ @dataclass 데코레이터의 frozen 인자를 사용해 불변 클래스를 만드는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from dataclasses import dataclass @dataclass(frozen = True) class Student: id : int name : str student1 = Student(100, "홍길동") student1.id = 200 """ Traceback (most recent call last): File "/home/king/testproject/main.py", line 10, in <module> student1.id = 200 File "<string>", line 4, in __setattr__ dataclasses.FrozenInstanceError: cannot assign to field 'id' """ |