-3

Is there any logic why it is acceptable to have variables with capital letters (e.g. myName = "Jason") inside if __name__ == "__main__": but not inside def main():?

EDIT : as apparently there is confusion, I got this conclusion by activating PEP 8 warnings and finding out that I didn't have warning in one case but had them in the other case : enter image description here

Code to reproduce the behaviour:

def print_hi(name):
    myName = "Jason"
    print(myName)


if __name__ == '__main__':
    myNameTest = "JasonTest"
    print(myNameTest)
  • 2
    It's not acceptable. Python is conventionally snakecase, not camelcase. – Barmar Mar 03 '22 at 16:50
  • 1
    How did you get to this conclusion? From my experience all python variables should be snake cased (eg `my_variable`) no matter where they are defined. The only exception I can see are global constants which should be uppercase (eg `MY_CONSTANT`). Take a look at https://www.python.org/dev/peps/pep-0008/#function-and-variable-names – Matteo Zanoni Mar 03 '22 at 16:51
  • I've been able to reproduce the behaviour on another laptop with PyCharm as well. It looks like it's PyCharm related @Barmar. Any idea how to fix it? Should I email PyCharm directly? – FluidMechanics Potential Flows Mar 04 '22 at 09:07
  • Yeah, you could write to JetBrains support or post at their discussion forum. – Barmar Mar 04 '22 at 10:37

5 Answers5

4

PEP8 doesn't make this dictinction.

Global Variable Names
(Let's hope that these variables are meant for use inside one module only.) The conventions are about the same as those for functions.
...
Function and Variable Names
Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

Maybe the places where you saw camelcase used were instances of the last exception.

Barmar
  • 741,623
  • 53
  • 500
  • 612
4

I think this is an issue with your IDE (PyCharm I'm guessing). PEP8's guidance on variable names doesn't distinguish between inside and outside of functions. Here's a link to the relevant section of the PEP8 spec.

rborchert
  • 146
  • 1
  • 6
1

All variables in python are conventionally written in snake_case. Only GLOBAL_CONSTANTS are written in caps. In your case, I believe the IDE will only remind you of that once, after that it will not, assuming you are making the code consistent by following one style of naming variables.

InfoDaneMent
  • 326
  • 3
  • 18
  • PyCharm will always warn you of non-snakecase variables in functions unless you specifically tell it to "ignore errors like this" (ALT+ENTER whilst cursor is on the variable in question) or completely turn off "PEP8 naming convention violation" in its settings (Editor -> Inspections). – bigkeefer Feb 25 '23 at 10:30
0

Style guides are just style and guides and sometimes their only 'reasoning' is that someone picked a convention. I actually do not like the PEP 8 convention that function names and variables names follow the same convention but that's just an opinion. I like my classes to be upper camel, my function names to be lower camel and my variables to be snake, but nothing prevents you from doing what you like. If you're working for a company, they might have specific guidelines like PEP 8. In general, style guides exist so that an entire code base will look the same and give you some instant familiarity with code that you may not have looked at for awhile (or that you may not have written). My company has even deeper conventions that allow knowing the preferred type of a variable!

Shaun Ramsey
  • 562
  • 4
  • 14
0

This is definitely a PyCharm "issue"; assuming it's not done by design for some reason known only to JetBrains -- which I'm thinking it probably actually is.

The warning in PyCharm (2022.3.2) specifically says: "Variable in function should be lowercase".

As you say however, PyCharm (with PEP 8 inspections enabled) has no problem at all with how you define global variable names. That is to say, they don't check the case at all.

For PEP 8 coding style violations it uses pycodestyle as a bundled tool.

PEP 8 naming convention violations, however, is built-in to PyCharm. i.e. JetBrains code it.

So, as @Barmar commented some time ago, I think the only way you're going to get an answer to this is to ask JetBrains directly (Support), or raise the issue in their forum. (I can find no evidence of anybody else ever having asked this on the web at all, so it would be a good question for them I think.)

bigkeefer
  • 576
  • 1
  • 6
  • 13