Getters and Setters in Python (Summary)
Now you know what getter and setter methods are and where they come from. These methods allow access and mutation of attributes while avoiding API changes. However, they’re not so popular in Python because of the existence of properties. Properties allow you to add behavior to your attributes while avoiding breaking changes in your APIs.
Even though properties are the Pythonic way to replace traditional getters and setters, properties can have some practical drawbacks that you can overcome with getters and setters.
In this video course, you’ve learned how to:
- Write getter and setter methods in Python
- Use Python properties to replace getter and setter methods
- Use Python tools, like descriptors, to replace getters and setters
- Decide on when setter and getter methods can be the right tool for the job
To learn more about the concepts in this course, check out:
- Managing Attributes With Python’s
property()
- Using Python Class Constructors
- Python Descriptors: An Introduction
- Inheritance and Composition: A Python OOP Guide
- Raising and Handling Python Exceptions
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Summary. Well done. You’ve made it to the end of the course. You now know what getter and setter methods are and where they come from. These methods allow access and mutation of attributes while avoiding API changes. However, they’re not so popular in Python because of the existence of properties.
00:18 Properties allow you to add behavior to your attributes while avoiding breaking changes in your APIs. Even though properties are the Pythonic way to replace traditional getters and setters, they can have some drawbacks which getters and setters can overcome.
00:33 In this course, you’ve learned how to write getter and setter methods in Python, use Python properties to replace getter and setter methods, use Python’s descriptors to replace getters and setters, and to decide on when getter and setter methods can be the right tool for the job.
00:50 With all this knowledge, you can be confident when to use properties or getters and setters in your Python classes. We hope you found this course useful, and we’ll see you again soon at realpython.com.
Become a Member to join the conversation.