Managing Attributes With Python's property() (Summary)
A property is a special type of class member that provides functionality that’s somewhere in between regular attributes and methods. Properties allow you to modify the implementation of instance attributes without changing the public API of the class. Being able to keep your APIs unchanged helps you avoid breaking code your users wrote on top of older versions of your classes.
Properties are the Pythonic way to create managed attributes in your classes. They have several use cases in real-world programming, making them a great addition to your skill set as a Python developer.
In this video course, you learned how to:
- Create managed attributes with Python’s
property()
- Perform lazy attribute evaluation and provide computed attributes
- Avoid setter and getter methods with properties
- Create read-only, read-write, and write-only attributes
- Create consistent and backwards-compatible APIs for your classes
If you’d like to learn more about the concepts covered in this course, then check out:
property()
- Python documentation- Properties - Python documentation
raise
- Python documentation- Logging in Python - Real Python tutorial
- Primer on Python Decorators - Real Python tutorial
- Write More Pythonic Code - Real Python learning path
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. A property is a special type of class member that provides functionality that’s somewhere in between regular attributes and methods.
00:14 Properties allow you to modify the implementation of instance attributes without changing the public API of the class. Being able to keep your APIs unchanged helps you avoid breaking code your users wrote on top of older versions of your classes.
00:29 Properties are a Pythonic way to create managed attributes in your classes. They have several use cases in real-world programming, making them a great addition to your skill set as a Python developer.
00:41
In this course, you learned how to create managed attributes with Python’s property()
, perform lazy attribute evaluation and provide computed attributes, avoid setter and getter methods with properties, create read-only, read-write, and write-only attributes, and create consistent and backwards-compatible APIs for your classes.
01:05
You also wrote several practical examples that walked you through the most common use cases of property()
. Those examples included data validation, computed attributes, logging your code, and more.
01:18 We hope you found this course useful and we’ll see you again soon at realpython.com.
Become a Member to join the conversation.