8

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 ...

3
  • 1
    Underscore and non-underscore thing I understand in your question but in the last line its totally on you that you want to name your variable output_path or output_filepath Commented Apr 19, 2018 at 8:41
  • 1
    Whatever you and maybe others working in the code prefer. Commented Apr 19, 2018 at 8:42
  • 1
    Personally I prefer output_path and out_path. The others feel long winded, especially if you have multiple variables that define different output paths. Though if you're editing someone's script then I'd say consistency is key to follow their conventions. Commented Apr 19, 2018 at 8:58

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.