Getter and Setter Methods
00:00 Getter and Setter Methods. When you define a class in object-oriented programming, you’ll likely end up with some instance and class attributes. These attributes are just variables that you can access through the instance, the class, or both.
00:16 Attributes hold the internal state of objects. In many cases, you’ll need to access and mutate the state, which involves accessing and mutating the attributes. Typically, you’ll have at least two ways to access and mutate attributes.
00:30 You can either access and mutate the attribute directly or use methods to access and mutate the attribute. If you expose the attributes of a class to your users, then those attributes automatically become part of the class’s public API.
00:46 They’ll be public attributes. This means that your users will directly access and mutate the attributes in their code. Having an attribute that’s part of a class’s API will become a problem if you need to change the internal implementation of the attribute itself.
01:03 A clear example of this issue is when you want to turn a stored attribute into a computed one. A stored attribute will immediately respond to access and mutation operations by just retrieving and storing the data, whereas a computed attribute will run computations before such operations.
01:19 The problem with regular attributes is they can’t have an internal implementation because they are just variables. So changing an attribute’s internal implementation will require converting the attribute into a method, which will probably break your user’s code. Why?
01:54 These methods are commonly known as getter and setter methods, but you may also find them referred to as accessor and mutator methods. Getter and setter methods are quite popular in many object-oriented programming languages, and it’s pretty likely you’ve heard about them already.
02:12 As a rough definition, a getter is a method that allows you to access an attribute in a given class, and a setter is a method that allows you to set or mutate the value of an attribute in a class.
02:24 If an attribute is likely to change its internal implementation, then you should use getter and setter a methods. This pattern also suggests that public attributes should be used only when you’re sure that no one will ever need to attach behavior to them.
Implementing the getter and setter pattern requires making your attributes non-public and writing getter and setter methods for each attribute. Let’s say you need to write a
Label class with text and font attributes.
03:17 Then you define getter and setter methods for both attributes. Typically, getter methods return the target attribute’s value while setter methods take a new value and assign it to the underlying attribute.
In Python, the distinction is between public and non-public class members. If you want to signal that an attribute or method is non-public, then you should use the well-established Python convention of prefixing the name with an underscore (
_). But note that this is just a convention.
To sum the problem up, think back to the
Label example and imagine that you want to automatically store the label’s text in uppercase letters. Unfortunately, you can’t simply add this behavior to a regular attribute such as
05:23 Well, in Python, you’ll most likely use a property as you’ll learn later on. However, programming languages such as Java and C++ don’t support property-like constructs, or their properties aren’t quite like Python properties. Instead, you provide getter and setter methods, which offer a quick way to change the internal implementation of the attributes without changing the public API.
05:45 This is why these languages encourage you to never expose your attributes as part of your public API. Encapsulation is another fundamental topic related to the origin of getter and setter methods. In essence, this principle refers to the bundling of data with the methods that operate on that data. This way, access and mutation operations will be done through methods exclusively.
The principle also extends to restricting direct access to an object’s attributes, which will prevent exposing implementation details or violating state invariance. To provide
Label with a newly required functionality in Java or C++, you must use getter and setter methods from the beginning.
In this version of
Label, you provide getter and setter methods for the label’s text. The attribute holding the text is non-public because it has a leading underscore in its name, and the setter method does the input transformation, converting the text into uppercase letters.
07:45 Even though the getter and setter pattern is quite common in other programming languages, that is not the case in Python. Adding getter and setter methods to your classes can considerably increase the number of lines in your code.
07:56 Getters and setters also follow a repetitive pattern that will require extra time to complete. This can be error-prone and tedious. You’ll also find that the immediate functionality gained from all this additional code is often zero.
and then later on in the code modifying it to a new value. Exposing attributes like
.font is common practice in Python, so your users will directly access and mutate this kind of attribute in their code.
09:14 So how do you deal with situations that require adding behavior to your attributes? The Pythonic way to do this is to replace attributes with properties, and that’s what you’ll see in the next section of the course.
Become a Member to join the conversation.