When is a variable considered constant in terms of PEP8 naming styles?

2024/11/17 15:49:47

In keeping with PEP8 conventions, in a .py I can define constants as:

NAME = "Me"
AGE = "Old"
GENER = "Male"

If a .txt contained Me Old Male on a single line, and in another .py I performed:

FILE = "C:/path/to/file.txt"  # a declared constant, easy
with open(FILE, 'r') as f:content = f.read().rstrip('\n').split()data = ','.join(content)  # returns Me,Old,Male

Question(s):

Can content and data be considered constants?

To be constant, must a variable be declared as a constant at build?

Or is constant vice variable a function of the ability to be altered by user input in runtime?

Supporting Informaton:

content is what is in the file, but it is subject to .rstrip() and .split() but it as a whole is never changed later. data is made from content, which hasn't changed and wont, and is subject to .join(). Neither values change after they are initialized.

I would view this similar to:

>>> A = 2  # a declared constant
>>> B = 2  # another declared constant
>>> TOTAL = A + B  # 'caps' per PEP8 for constant naming
4

Assuming the program has terminated and TOTAL is never altered, I would consider this value a constant. Again under the presumption that any variable that is not alterable during runtime is to be considered a constant.

Feel free to alter my notions as required to align with standards!

Answer

If you are going to treat the value as a constant in the rest of your code, by all means, use CONSTANT_CASE for those globals. It's up to you, it's merely a documentation convention.

In other words, it's a convention that aims to make it easier for future readers of your code to understand that the value of such a global is set just once and is expected not to change over the lifetime of the program.

Note that I'd generally try to avoid loading file data on module import; it makes it harder to test and impacts performance. Load that data on first use instead (using a function).

https://en.xdnf.cn/q/71200.html

Related Q&A

Average Date Array Calculation

I would like to get the mean of the following dates. I thought about converting all the data to seconds and then averaging them. But there is probably a better way to do it.date = [2016-02-23 09:36:26,…

DRF: Serializer Group By Model Field

I want my api to return Account objects grouped by the account_type field in the model. I want to do this so that the grouped accounts are easier to access in my JS code. This is what is returned now:[…

Need help combining two 3 channel images into 6 channel image Python

I am trying to combine two different RGB images into a single 6 channel image (Tiff is best) using nothing but Python.What I have is an RGB image taken from a normal camera as well as another RGB image…

Komodo Edit disable autocomple

I am using Komodo Edit 8 and its autocomplete feature is totally annoying. As soon as I type "for i" , it autofills in this:for i in range:codeNow i have to delete it manually to continue typ…

Python WeakKeyDictionary for unhashable types

As raised in cpython issue 88306, python WeakKeyDictionary fails for non hashable types. According to the discussion in the python issue above, this is an unnecessary restriction, using ids of the keys…

Django MPTT efficiently serializing relational data with DRF

I have a Category model that is a MPTT model. It is m2m to Group and I need to serialize the tree with related counts, imagine my Category tree is this:Root (related to 1 group)- Branch (related to 2 g…

Psycopg2: module object has no attribute connect [duplicate]

This question already has answers here:Importing a library from (or near) a script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError(4 answers…

matplotlib text not clipped

When drawing text in matplotlib with text(), and then interactively panning the image, the resulting drawn text is not clipped to the data window. This is counter to how plotting data or drawing text …

how to make child class call parent class __init__ automatically?

i had a class called CacheObject,and many class extend from it.now i need to add something common on all classes from this class so i write thisclass CacheObject(object):def __init__(self):self.updated…

Creating a dataframe in pandas by multiplying two series together

Say I have two series in pandas, series A and series B. How do I create a dataframe in which all of those values are multiplied together, i.e. with series A down the left hand side and series B along t…