When is a variable considered constant in terms of PEP8 naming styles?
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).
madeslurpy
Updated on July 09, 2022Comments
-
madeslurpy almost 2 years
In keeping with PEP8 conventions, in a
.py
I can define constants as:NAME = "Me" AGE = "Old" GENER = "Male"
If a
.txt
containedMe 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
anddata
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 fromcontent
, 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!
-
madeslurpy almost 7 yearsI like to be thorough and inline with practice standards. Thanks for the clean response! To follow up, why is having a file path declared in a program bad practice? It is used in a script that is designed to have user data auto-gathered each time the run the script so they don't have to supply a path to their info each time.
-
Christian Dean almost 7 yearsI think your missing the premise of his question(as did I). He is asking what are the general guidelines for when you should make a variable "constant" in Python. Not how constant-ness could be implemented.
-
madeslurpy almost 7 yearsWhat is meant by "Load that data on first use instead (using a function)?
-
Martijn Pieters almost 7 yearsTest if that data is read when you need access to it, and if not, then read it and cache the data. Put that logic in a function so you can test it with a unittest.
-
madeslurpy almost 7 yearsSo declaring the path as a constant is fine on load, but I don't get why I cant then just sequentially read the data into a variable? What if that is the first thing I need to do? Run
.py
, imports modules, then has to read that file to getdata
before moving on. Are you suggesting to check ifdata
isNULL
and if so, then go read? -
madeslurpy almost 7 yearsAppreciate the response but not in the scope of what I was asking. I've read about this by Alex Martellis AS.
-
Martijn Pieters almost 7 years@madeslurpy: yes, but now importing your module incurs extra I/O on import, increasing the risk that something goes wrong at that time and making it harder to write tests for your code (certainly for the code that loads the data).