Concept

The Naming of Constants

In Python Programming, it is customary to give constants an all-caps variable name in order to help others looking over the code. Take an example where we are using a for loop to calculate the average value in a list of 20 values.

average=0 for i in values: average+=(i/20)

In this example, it is unclear where we got the 'magic constant' 20. However, if we use Python Style Guidelines:

average=0 NUM_VALS=20 for i in values: average+=(i/NUM_VALS)

In this second example, it is more explicit what the numbers are doing within the operation. Thus, this is the preferred way to deal with constants in Python.

0

1

Updated 2021-05-28

Contributors are:

Who are from:

Tags

Python Programming Language

Data Science