Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - define constant inside function

Given that there are no real constants in Python, the convention is to name them in CAPS for conveying the intentions.

In following sample code, FIRST and SECOND are constants:

def fibonacci_generator(count):
    FIRST, SECOND = 0, 1
    a, b = FIRST, SECOND
    for _ in range(count):
        yield a
        a, b = b, a + b

print(list(fibonacci_generator(10)))

But for the two constants, PyCharm is giving warning as:

Variable in function should be lowercase

enter image description here

Is there any other correct way to define constants within functions? (Without suppressing the PyCharm warning)

like image 614
Sagar Gupta Avatar asked Sep 05 '19 08:09

Sagar Gupta


People also ask

How do you define a constant in Python?

However, Python doesn't have a dedicated syntax for defining constants. In practice, Python constants are just variables that never change. To prevent programmers from reassigning a name that's supposed to hold a constant, the Python community has adopted a naming convention: use uppercase letters.

Is there #define in Python?

In python, a function is a logical unit of code containing a sequence of statements indented under a name given using the “def” keyword. In python def keyword is the most used keyword. Use of def keyword: In the case of classes, the def keyword is used for defining the methods of a class.

Is constant a function in Python?

A constant is a type of variable that holds values, which cannot be changed. In reality, we rarely use constants in Python. Constants are usually declared and assigned on a different module/file. Then, they are imported to the main file.

How do you declare a global constant in Python?

The global keyword in Python is used to modify a global variable in a local context (as explained here). This means that if the op modifies SOME_CONSTANT within myfunc the change will affect also outside the function scope (globally).


2 Answers

According to PEP8 constants should be defined at the module level:

Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.

There is no convention for naming constants inside functions or methods.

You can:

  • Live with the warning, or
  • Suppress it, or
  • Use "normal" lowercase names

In this case, you could also use default arguments without getting a warning but it does seem like an overkill just to get around a PEP8 convention warning:

enter image description here

However, this is counter-productive because you violate one convention in order to not get a warning about violating another.

like image 113
DeepSpace Avatar answered Oct 20 '22 01:10

DeepSpace


Besides what have been proposed by @DeepSpace, other potential choices are (1) using underscore-prefixed variable names such as _FIRST, _SECOND, etc.; (2) writing a class instead and make the constants class-level constants.

like image 4
GZ0 Avatar answered Oct 20 '22 02:10

GZ0