From the course: Python Object-Oriented Programming
Unlock the full course today
Join today to access over 24,500 courses taught by industry experts.
Attribute access - Python Tutorial
From the course: Python Object-Oriented Programming
Attribute access
- [Instructor] Python's Magic methods also give you complete control over how an object's attributes are accessed. Your class can define methods that intercept the process anytime an attribute is set or retrieved. So here in chapter 3, let's open up the magic attributes start and let's see how this works. So here in the code, I have my book class which defines some attributes and it also overrides the str function that we learned about earlier. So let's start off by seeing how we can control when an attribute is retrieved. Python lets us define a method called underscore underscore get attribute, which is called whenever the value of an attribute is retrieved. So let's add that. Okay. And I'll get rid of some of this extraneous stuff in here. Alright. This gives us the opportunity to perform any operations on the value before it is returned. So, for example, you can see that we have an internal attribute here…