Nonpublic Variables and Best Practice of Getters&Setters in Python

Yunus Emre Gündüz
5 min readMar 12, 2023

--

In the concept of defensive programming, the program architecture is designed to restrict access to methods and variables, and sometimes even classes, that are not needed by other parts of the program. This approach helps in maintaining the program and reduces the likelihood of errors. Encapsulating variables using access modifiers, instead of directly accessing them, is considered a better practice. However, there may be instances where non-public variables need to be accessed within the program, and in those cases, getter and setter methods are created. In this article, those are briefly discussed:

1 . Non-public variables in Python compared to Java’s private

2. The best practice of getters&setters usage

3. Short representation of creation of model classes for different purposes and frameworks

Understanding Python’s Non-Public Variables

Before delving into getters and setters, it’s important to understand the differences between how private variables are declared in Python and Java.

As seen in our model class Post, both variables are declared as private, meaning that they cannot be reached outside of the Post class. However, in Python, there is not a natural way to create completely private variables. Instead, a single underscore _ is used to indicate that the variable should not be accessed directly outside of the class. However, it is still possible to reach them.

In Python, variables are categorized into public and non-public, but they are not completely non-public. The use of a single underscore is just a convention, similar to the use of capital letters to indicate that a variable is a constant. While it is possible to access non-public variables in Python, it is generally considered a bad practice to do so, and programmers should instead use getter and setter methods to access and modify the values of these variables.

I have come across some Python programmers who do not require extensive knowledge of OOP concepts, yet they use double underscores (__) to create private variables. At first glance, it may appear that a variable with a double underscore prefix cannot be accessed. However, they are not truly private, and the double underscore has a distinct purpose known as Name Mangling. If you have been using double underscores to create private variables, I strongly recommend consulting the official documentation for a better understanding. You can find it here.

Use Decorators for Getters&Setters

The concept of getters and setters provides the programmer lots of benefits. One of them is to validate the variable before assigning a value to it. You might find more reasons to use getters&setters instead of directly changing and reading the variable. Have you ever know that in Python the way declaring getter/setter methods as in other object-oriented languages (such as Java, C#) is not recommended?

First, let’s remember the way used to create getters and setters.

Programmers switching to Python are generally keep their behaviour of declaring getters&setters as in the other languages.

This is what most beginners tend to do when they first start coding, I suppose. Although the code may work without any errors, in Python it’s not recommended to use this approach, as it may lead to potential errors if you decided to change the scope of an already existing attribute from public to private. Instead, it is recommended to use the @property decorator to define getters and @attr_name.setter to define setters.

As it is seen in the example, two methods with the attribute name is created. One of them is decorated as @property indicating the getter method, while the other is decorated as @attr_name.setter indicating the setter method.

In Java, getters and setters are invoked using the standard method call syntax shown below:

When it comes to using getters&setters in Python, it is little bit different though. Instead of using method calling syntax, the methods are called like this:

This technique may give you the impression that the methods are not being called, but in reality, the methods decorated with @property are being executed in the background. You can confirm this by placing a print statement inside the methods.

While Python recommends using this technique to avoid potential errors, it may make it more difficult to determine whether a variable is being directly assigned or if a getter/setter method is being used to manipulate the variable (e.g., performing validation). However, this approach does have the benefit of making it possible to handle a project in which previously non-public variables are made public without needing to make significant changes to the codebase.

Model Classes

Although creating a model class may not be as common in Python as in some other languages, there are still various ways and techniques to achieve this based on your requirements. One option you might consider is using the dataclasses module. This module provides the dataclass decorator which can be used to define a class with a concise syntax for defining class attributes.

Or if you want to post and get data from an SQL database using model classes, you may want to consider using ORM with SQLAlchemy and create a model class by inheriting it from DeclarativeBase class:

Or as in SQLAlchemy in Flask framework, by inheriting from db.Model class:

In conclusion, while the use of single underscores in Python indicates non-public variables, the convention of using double underscores has a distinct purpose known as Name Mangling. The recommended use of @property decorator and @attr_name.setter instead of traditional getters and setters in Python helps prevent errors and validate variable values. Depending on the purpose, different techniques such as dataclasses package or ORM with SQLAlchemy in Flask framework can be used to create model classes. Overall, it is important to follow best practices and stay up-to-date with the latest techniques to create efficient and effective programs.

I hope you’ve enjoyed and learned from the article.

--

--

Yunus Emre Gündüz

Physics undergrad student. Eager to learn and share. Writing about data engineering, cloud techs, social network analysis & all other geeky stuff.