Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to define constant variables python 3

Tags:

I am writing a program in python which contains many constant variables. I would like to create a file that will hold all these variables like the .h file in C that contains many #define. I tried to use configparser however I didn't find it easy and fun to use.

Do you know a better way?

like image 605
TomE8 Avatar asked May 25 '18 16:05

TomE8


People also ask

How do you define a constant in Python?

Assigning value to constant in Python In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.

What is the best way to write variables in Python?

Python allows you to name variables to your liking, as long as the names follow these rules: Variable names may contain letters, digits (0-9) or the underscore character _ . Variable names must begin with a letter from A-Z or the underscore _ character. Either lowercase or uppercase letters are acceptable.

What is the correct way of declaring constant variable?

Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.


2 Answers

Python does not allow constant declarations like C or C++.

Normally in Python, constants are capitalized (PEP 8 standards) which helps the programmer know it's a constant.

Ex. MY_CONSTANT = "Whatever"

Another valid way of doing it which I don't use but heard of, is using a method:

def MY_CONSTANT():     return "Whatever" 

Now in theory, calling MY_CONSTANT() acts just like a constant.

EDIT

Like the comments says, someone can go and change the value by calling

MY_CONSTANT = lambda: 'Something else' 

but don't forget the same person can call MY_CONSTANT = "Something else" in the first example and change the initial value. In both cases it is unlikely but possible.

like image 162
scharette Avatar answered Oct 15 '22 03:10

scharette


Constants (in a sense) in Python 3.8+

Python 3.8 introduces the typing.Final type qualifier, which is used to indicate that a variable or attribute should not be reassigned, redefined, or overridden.

PEP 591 -- Adding a final qualifier to typing

from typing import Final  # Annotate module variables # (with or without an explicit type, using the syntax Final[<type>]) # (type is auto-determined in absence of an explicit type) PI: Final[float] = 3.141592654 ANSWER_TO_EVERYTHING: Final = 42   # Annotate instance variables in class bodies # (explicit type is needed if no value is assigned) class Point:     x: Final[int]     y: Final = 0      def __init__(self, x: int):         self.x = x   # Annotate instance variables directly # (only allowed in __init__ methods) class Person:     def __init__(self, birth_year: int):         self.birth_year: Final = birth_year 

Linters and type checkers will show you warnings if you reassign or redefine Final variables. Note that there is no runtime check, so you can still run the code below.

ANSWER_TO_EVERYTHING: Final = 42 ANSWER_TO_EVERYTHING = 420  # shows warning print(ANSWER_TO_EVERYTHING)  # prints 420 

There is also the typing.final decorator, which is used to restrict inheriting classes and overriding methods.

like image 22
Evening Hawk Avatar answered Oct 15 '22 05:10

Evening Hawk