Categories
Python Answers

How to create a constant in Python?

Spread the love

Sometimes, we want to create a constant in Python.

In this article, we’ll look at how to create a constant in Python.

How to create a constant in Python?

To create a constant in Python, we can use the typing.Final property.

For instance, we write

from typing import Final

a: Final = 1

to set variable a to the Final type.

This will be checked by static type checkers to see if a has been reassigned.

But it doesn’t prevent reassignment at run time.

Conclusion

To create a constant in Python, we can use the typing.Final property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *