From the course: Python Object-Oriented Programming

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Immutable data classes

Immutable data classes

- [Instructor] Occasionally you'll want to create classes whose data can't be changed. In other words, you want the data within them to be immutable. Python data classes make this possible by specifying an argument to the data class decorator. So let's open up immutable start. And you can see here that I have a simple data class with a couple of attributes, a string and an integer along with some code that creates the object and prints out the object's value. In fact, let's just go ahead and add both value one and value two. Okay, so before we do anything else let's just run the code to make sure it works. And sure enough, yes, it prints out value one and then the number zero. So to make this class immutable I can set the frozen argument to true in the data class decorator. So let's hide the terminal and inside the data class decorator here I'm going to write frozen equals true. So now this prevents any of the…

Contents