Getter and Setter methods in Python

Photo by Brittney Weng on Unsplash

hmm…, it’s already June and also this is my first article for year 2021 🧐. My plan for 2020 was to have at least 1 article/month. Hope you are doing fine.

During my early times of learning Object Oriented Programming (OOP), getter and setter method were some of the concepts I learned. These methods embody encapsulation property of the OOP paradigm. I was bit amused by classes having private and public property and how you want to keep your classes’ properties secure and mysterious by encapsulating it.

I have used this concept in some commercially successful languages like Java and C++. Some new niche languages that I had to learn in Formal Methods academic class also had this concept. So if you are modeling any phenomenon using OOP paradigm, there is a high chance that you’ll need to understand it. So let’s get started. 🙌

First we’ll see a generic way of implementing the methods and then pythonic way of implementing them.

In python, by convention private method or attribute starts with a single underscore _. We do not have explicit term such as private like in Java to indicate it. Let’s make class for a hamster and create an instance of it.

On running the file, we get:

Name: hoshi, Date of Birth: 2020-02-02

You can change name of hoshi instance by doing

hoshi._name = "newHoshi"

but that’s not recommended and is also called monkey patching; sorry, monkey. The change is reflected only in that instance of the class Hamster.

Now let’s add getter and setter methods so that we do not touch instance’s private attributes. So instead of hoshi._name we can do hoshi.get_name() , and hoshi.set_name("newhoshi”) instead of hoshi._name = “newhoshi".

On running the file, we get:

Name: hoshi, Date of Birth: 2020-02-02
hoshi's new name is newHoshi

The benefit of using setter and getter method is we don’t have to know what an instance’s attributes are named as and just focus on doing cool stuffs with an instance’s available data.

Let’s implement the getter and setter method pythonic way by using @property and @<property-name>.setter decorator.

One running the file, we get:

Name: hoshi, Date of Birth: 2020-02-02
hoshi's new name is newHoshi
hoshi's age is 1

What’s the difference between generic and pythonic way of implementing setter and getter methods? Well, first of all

pythonic way — refer hamster2.py
generic implementation — refer hamster1.py

In the pythonic way, we have called the setter method in the __init__ method itself which in turn sets value to the class’s private attributes _name and _dob .

By using the pythonic way of implementing setter and getter method, we have truly achieved full encapsulation.

One caution: If you are implementing getter and setter method using decorator, make sure your @property method is defined first, so that @<property-name>.setter decorator know which property name it is referring to else you’ll get NameError exception.

That is all for this article. Hope it was helpful and invigorated to explore!

My next article will be on __magic__ 🪄 ✨ methods! See you then.

Software Engineer