Set constant in Python

8

How can I declare a Python constant in the same way I do in C with

#Define PI 3.1415

or in java

public final double PI = 3.1415
    
asked by anonymous 05.04.2017 / 21:35

3 answers

9

In Python this is not possible, just create a variable and do not change its value.

PI = 3.1415

A simple workaround, if it really is much needed to have full assurance that this value will never be rewritten elsewhere in the code is to create a function that returns the value desired.

def const_pi():
    return 3.1415
    
05.04.2017 / 21:42
7

It does not. Do not forget that Python is a script language, so it does not need to supply all the mechanisms other languages have.

The way to do it is:

alguma_coia = 10 #não mexa neste valor, ele deve ser constante 
    
05.04.2017 / 21:41
4

As put in the other answers, Python, being a dynamic language, does not have the "constants" feature in a simple way: everything in Python is an object. When you define a literal number in Python code, this number is actually a constant - the bytecode compiler places the literal value of it in .pyc - only, by the nature of the language, any variable is just a name linked to an object - and at any time, you can simply link the name to another object.

In normal usage, it is assumed that "Python is a language designed to be used by adults who consent to what they are doing" - then a declared value in the body of a module, indicated by a name with capital letters, is , by convention, treated as a constant, and one should not change the value of that variable. Simple as that.

Understand that if you have these two lines in your code, the value 3.141592 still exists in memory. But the variable Pi will now refer to a float object in another memory location:

Pi = 3.141592
Pi = Pi + 1
Now, Python allows almost unbound customization of how data is accessed if they are attributes of a class (if they are variables of one module, the story is another, but hacks are possible).

Describing these mechanisms and suggesting various ways to create things like constants would be possible, but too advanced, and would be the size of a book chapter.

Instead, let's look at an example: the "Enum" standard library class can be used to create classes with "constants" inside - which in addition to a value that can not be changed directly, still has other properties such as "readable name", among others:

import enum

class Numeros(enum.Enum):
    pi = 3.141592

print (Numeros.pi, Numeros.pi.value)

displays:

(<Numeros.pi: 3.141592>, 3.141592)

And try to do:

Numeros.pi = 4

results in:

AttributeError: Cannot reassign members.

This Enum engine uses Python's dynamism to do something you can not do in languages that have the "normal" constants you mention: at runtime you can use the name of the constant. That is: in C it is impossible for you to print "PI" if you use #define PI 3.141592 - since it defines it as a search and replace. Some libraries have support for using the Enum of C in order to look at the name at runtime (I believe) - but in Python, just do:

print(Numeros.pi.name)

To see the printed "pi" text. And if you want to use the numeric value, you must use the "value" attribute: print(Numeros.pi.value)

    
05.04.2017 / 23:18