Python Equivalent to Java Final Keyword

In Java, the ‘final’ keyword is used for variables that cannot be reassigned to an object. Python being a dynamically typed language, earlier was short of this feature. But from the 3.8 version of Python, we do have a feature named “Final” from the typing module. However, we will also look into other tools present in Python, that will help us achieve a complete concept of immutability.

The ‘Final’ keyword, introduced in version 3.8, serves as an equivalent to Java’s final keyword, offering a way to declare variables and methods immutable. While it does not enforce immutability at runtime, it signals to type checkers like Mypy that certain elements should not be modified. This article explores Python’s approach to immutability, including alternatives like tuples and frozen sets, offering insights into achieving data constancy similar to Java’s final keyword.

Also Read: What is The Python Equivalent of ~/.bashrc?

Understanding the ‘final’ Keyword in Java

In Java, the ‘final’ keyword serves the following purposes:

Final Keyword in Java: Variables and Constants

Java allows users to declare variables and attributes as final. Once declared and assigned, they cannot be modified ahead in the program.

Java’s Final Keyword: Methods and Attributes in Sub-Classes

Java allows us to declare methods and attributes of classes as Final. When classes are inherited, Java doesn’t allow subclasses to override existing final methods of the parent class. Thus it is one of the properties of the final keyword to stop users from redefining methods declared as final.

Final Classes Cannot be Inherited

Similarly, final classes cannot be inherited by other classes in Java. The functions and attributes inside final classes are also final. Java also prevents extending such classes via interfaces.

Python’s Final Keyword Equivalent

From Python 3.8, the Final keyword was introduced in the ‘typing’ module of Mypy. Mypy is an optional type checker that is used to type-check the Python program.

Mypy and the ‘Final’ Typing in Python: To run the typing module, one should install Mypy using pip in their systems. Head over to references for more details.

Now, we look at some of the properties exhibited by the Final keyword.

Demonstrating Final Constants and Variables in Python

Likewise, Java, and Python allow users to declare variables and constants as Final. Once declared, they cannot be redefined, reassigned or overridden. This prevents modification at modular and class levels.

For example:

from typing import Final
# Variable declared as Final
MAX: Final = 9000
MAX = 1

class A:
# Attribute declared as Final
    TIMEOUT: Final[int] = 10

class B(A):
    TIMEOUT = 1  
Compilation_Error
Compilation_Error

Here we have two cases:

  • Variable declared as Final: The variable MAX is declared Final, which prevents us from assigning a new value in the third line.
  • Attribute defined as Final: Inside class A, an attribute “TIMEOUT” is defined as Final. After the class B inherits class A, the attribute “TIMEOUT” is also inherited as a property. But as it was declared Final, Python doesn’t allow us to assign a new value to the attribute.

    Illustrating Final Methods in Python

    Similarly, Mypy allows users to declare class functions, static functions, and instance functions as Final.