From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach

Unlock the full course today

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

Setters

Setters

- [Instructor] We are now going to look at setters. You can directly update properties of Python objects. However, you may want to apply some kind of logic to that update process. For example, if we have a def set_account_number, and this will take self and an account number, which we pass in. Then directly updating it would look like this. You'd simply do self._account_number is equal to the parameter account_number. And that would work fine. However, you might want to apply some kind of logic to this update process. So what we're going to do instead is we're going to do if isinstance, so this means if this particular object is an instance of a string, then we'll go ahead and we'll update that property. However, if it's not, then we'll simply print an error message. Okay, so let's go ahead and see how this works. So down on line 35, I'm going to try and update the account number, so my accounts, dot, and you'll see in this list we've now got the set account number method that we just…

Contents