Created
July 10, 2018 14:06
-
-
Save tal95shah/e34f784a745124e7706544e32d7d380c to your computer and use it in GitHub Desktop.
Immutable Dataclass Employee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| @dataclass(frozen=True) | |
| class Employee(object): | |
| id: int | |
| name: str | |
| if __name__ == '__main__': | |
| emp = Employee(1,"Talha Gillani") | |
| emp2= Employee(100,"Syed Talha Gillani") | |
| print("emp:{}".format(emp)) | |
| try: | |
| emp.id = 100 | |
| except Exception as errorMessage: | |
| print("\nError:{}\n".format(errorMessage)) | |
| print("emp:{}".format(emp)) | |
| print("emp2:{}".format(emp2)) | |
| print("emp == emp2:{}".format(emp == emp2)) | |
| print("emp != emp2:{}".format(emp != emp2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment