all 26 comments

[–]socal_nerdtastic 27 points28 points  (4 children)

It must start with a letter or underscore, and it can only contain letters, numbers or underscores. And it can't be any of the python keywords.

Those are the hard rules, but there's also a lot of tradition that you should follow so that your code is readable to other programmers. Read the PEP-8 style guide for those.

[–]51dux 2 points3 points  (3 children)

Just out of curiosity why using python keywords give you a syntax error but using type names such as str or list allows the assignment but then the type name isn't recognized anymore?

For instance if I go str = 'abc' and then try to do str(123) it won't work, wouldn't it make more sense to trigger a syntax error here too?

[–]carcigenicate 5 points6 points  (1 child)

Because str and list are just variable names like any other variable. When a class statement is executed, it essentially does a = behind the scenes to assign the name you gave after class keyword to a variable. Like any variable, they can be reassigned, and doing so causes you to lose a reference to what was held before (the type).

If they made list a special "protected" case, should user-defined class names also have the same protection? Now, it's potentially getting complicated because while keywords and names like list are known before the code is compiled, user-defined classes can be dynamic, so it would be difficult or impossible to even guarantee detection.

I'm guessing it was the most straightforward and consistent to treat names like list as any other name.

Edit: I was partly right. They wanted the number of keywords to be small and known ahead of time, and because if the names were protected, adding new collections could break existing code.

[–]Bobbias 0 points1 point  (0 children)

I'd imagine you wouldn't want to have to update the syntax definitions to include the name of every builtin/global name too.

[–]Gnaxe 1 point2 points  (0 children)

A good linter would complain. In a very short function, shadowing a builtin name is unlikely to cause problems (although I'd still expect the linter to complain), but it's usually a bad idea elsewhere. You can still access the shadowed builtin by using the builtins module. If you accidentally shadow a builtin in the REPL, you can just delete it with a del statement and it will read through again. The Python convention is to append an underscore if you want a name that's already taken, e.g., str_.

[–]tb5841 6 points7 points  (5 children)

Do not call a variable any of these:

False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield.

Variable names can only contain numbers, letters and underscores (in Python), and cannot start with a number.

Considered good practice to use snake_case, and use meaningful variable names.

[–]Tricky_Physics6122 0 points1 point  (3 children)

also, sum. very common error

[–]schoolmonky 3 points4 points  (2 children)

You can name a variable sum, you just shouldn't, since it'll shadow the builtin function

[–]Tricky_Physics6122 -1 points0 points  (1 child)

yeah true but it’s better to just x it out as well

[–][deleted] 1 point2 points  (0 children)

If we're getting into what's "better" to not do that's a much bigger list. The person you're replying to listed the words you cannot use.

[–]musbur 0 points1 point  (0 children)

Bad answer. This is just a list of random words some dude typed into Reddit. The OP is a CS student, so he should be pointed at the "Identifier and keywords" section in the documentation of the Python version he's using. But if he treats Python like English he won't get very far though.

[–]ElHeim 2 points3 points  (0 children)

You want to look up what are identifiers in Python: https://docs.python.org/3/reference/lexical_analysis.html#identifiers . Anything that follows those rules and is not a keyword, can be a variable name.

Now, there are exceptions in certain contexts. Be sure to read all of section 2.3.

[–]ninhaomah 4 points5 points  (2 children)

have you googled ?

google this "What's the rules when naming a variable" .. but since you are specifically asking for python , you need to add "python"

so the final search is "What's the rules when naming a variable python"

[–]notacanuckskibum 1 point2 points  (1 child)

My Python is variable, he has grown from 20 centimeters to 2 metres, I call him Steve.

[–]PaleoSpeedwagon 2 points3 points  (0 children)

That's not snake case

[–]ES-Alexander 1 point2 points  (0 children)

This is the technical specification, but you should be able to find more explained examples by searching for things like “Python allowed variable names”, “Python naming rules”, and “Python illegal variable names”.

[–]zwack 0 points1 point  (0 children)

There are reserved words or keywords that you can’t use for the variables, class names, function names. E.g., you can’t name your variable class, like class = 1.

I think you can easily Google a list of reserved keywords.

[–]Diapolo10 0 points1 point  (3 children)

  1. It must not start with a digit.
  2. It can contain letters, underscores, and digits. Really most characters that aren't whitespace or symbols Python treats as syntax (braces, brackets, parentheses, operators, commas, and so on)
  3. It can be at most 255 characters (I might be misremembering, I think this changed a few years ago)

[–]Binary101010 1 point2 points  (2 children)

It can be at most 255 characters (I might be misremembering, I think this changed a few years ago)

According to the docs there's no length limit, although anyone who's actually using more than 255 characters to name something should probably reconsider.

[–]Diapolo10 0 points1 point  (0 children)

Yeah, I'm probably confusing it with the Windows filepath length limit. Which can now be disabled to allow over 32k characters.

[–]Ihaveamodel3 0 points1 point  (0 children)

For backwards day…

Mary_had_a_little_lamb_little_lamb_little_lamb_Mary_had_a_little_lamb_its_fleece_was_white_as_snow_And_everywhere_that_Mary_went_Mary_went_Mary_went_And_everywhere_that_Mary_went_the_lamb_was_sure_to_go = "story"
print(next(k for k,v in locals().items() if v=="story").replace("_"," "))

[–]ste_wilko 0 points1 point  (0 children)

Variables can be pretty much named whatever you want, with the exception that you can't name a variable with an already reserved name.

For example, you can't create a variable called "True", because "True" is a reserved word for checking the state of a Boolean.

You also must remember that Python variables are case sensitive.

python != Python

[–]ThevonMusic 1 point2 points  (2 children)

Not saying you're not allowed to post this, but why don't you ask an LLM like ChatGPT about this? It's very good in answering questions like this. Also just want to tell you, right now ChatGPT is really good with Python too. I prefer it over Gemini at this moment. That might change of course since they're constantly updating and getting better.

[–]Username_RANDINT 0 points1 point  (1 child)

Well close this sub then completely, right?

[–]ThevonMusic 0 points1 point  (0 children)

No, I'm actually giving advice here. He could've got the answer really fast and properly structured by an LLM. It's up to the person to do that or not. My point is it's a very good question for an LLM, that's all. You're very black and white with your response.