I would like to know the correct naming convention for the following variables in Python which I couldn't find one from Google Style Guide and PEP8
(Let's say I have the following Python code)
output_file = open(output_file_path, 'w')
What would be the best variable name for the out file name?
I believe the possible options for the variable name would be something like
output_file
outputfile
outfile
out_file
outfile
And the path variable can be something like
output_file_path
output_filepath
output_path
out_path
...
Rules for Python variables: A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
filepath, as an unhyphenated compound word, is generally used when discussing it as an entity (e.g., “You'll need to set the filepath before writing out any data.”), but file path, as two words, is generally used when referring to a particular attribute of a file.
In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.
According to PEP8 you should use _
between each meaningful words for variable names, Similarly we use capital case for class names.
by searching about the word filepath
I should say that there is not such a word in English, it means that it is not a single word, it contains two separate word(file
, path
), so it is correct to use file_path
instead of 'filepath', although both of them is being used by developers these days.
About the part that contains output
word, According two Zen Of Python we already knew that:
Readability counts.
and
Explicit is better than implicit.
I should say that is pretty much better to use output
before your variable name.
So I think output_file_path
and 'output_file' are the correct and best choices here.
Citing from https://english.stackexchange.com/a/428231/289175 :
Both forms, filepath and file path, are used, but which one is used is often dependent on context. [...] filepath, as an unhyphenated compound word, is generally used when discussing it as an entity (e.g., “You’ll need to set the filepath before writing out any data.”), but file path, as two words, is generally used when referring to a particular attribute of a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With