Python Forum

Full Version: How long can an identifier be in Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What is the usual length of a python identifier? Are there any rules regarding it?
Guessing: Your memory limits the length of a name.
In Python, an identifier can be of any length. Apart from that, there are certain rules we must follow to name one:

*It can only begin with an underscore or a character from- A-Z or a-z.
*The rest of it can contain anything from the following: A-Z/a-z/_/0-9.
*Python is case-sensitive, as we discussed in the previous question.
*Keywords cannot be used as identifiers. Python has the following keywords:
and, def, False, import, not, True,
as, del, finally, in, or, try,
assert, elif, for, is, pass, while,
break, else, from, lambda, print, with,
class, except, global, None, raise, yield,
continue, exec, if, nonlocal, return,
So, we can make all the SingletonAbstractAbstractFactoryFactories we want but um... let's not.
I just tried a 10MB name and there was no problem. Only a small delay in the compiling step
>>> name = "a" * 100000000
>>> assign = "{} = 3".format(name)
>>> exec(assign)
>>> print(eval(name))
3
Reminds me of working in SAS, which has (at least until recently) a limit of 32 character names. I was also using an SQL database made by people who liked to make really long names instead of documenting anything, like ClericalCodingCompleteDateTime which has 33 characters. Although the latest version they installed, right before I left that job, allowed names with spaces in them. IIRC, the catch was that you had to quote them and precede it with an n. So n'credit card' was a valid variable name.