Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can variable names in Python start with an integer?

This is somewhat academic, but nevertheless.

Python syntax forbids starting a variable name with a number, but this can be sidestepped like so:

>>> globals()['1a'] = 1
>>> globals()['1a']
1

Likewise for locals().

Does that mean that Python actually allows it, and that it's just not very visible?

Edit:

My question is not whether it is allowed; I am aware that it is formally not allowed in Python. The question is why can I work around it by addressing globals() directly, and if that breaks certain rules or guidelines, or if it has a good reason/application to allow that.

like image 851
steffen Avatar asked Jan 31 '17 16:01

steffen


People also ask

Why can't variable names start with a number python?

That said, pretty much all of the time a language doesn't allow variable names to begin with numbers is because those are the rules of the language design. Often it is because such a simple rule makes the parsing and lexing of the language vastly easier. Not all language designers know this is the real reason, though.

How do you start a number with a variable in Python?

A variable cannot start with a number but can start with a letter or an underscore. Python also uses a list of reserved keywords that hold a special meaning. For example, the word “def” is used to define functions. It is possible for variables to use keywords as names, but it is not good practice.

Can Python variables be numbers?

Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters ( A-Z , a-z ), digits ( 0-9 ), and the underscore character ( _ ). An additional restriction is that, although a variable name can contain digits, the first character of a variable name cannot be a digit.


2 Answers

Python parser forbids naming variables that way, for the sake of parsing numbers and variables separately, as naming a variable 1e1 would create a chaos - is it the number 10.0 or the variable 1e1?

"Python, please output for me 1e1!" - "Why is it 10.0? I stored 100 over there!"

But the variables are actually stored in a way that allows binding a string that starts with a number to a value, because that feature is no harm in hashing maps of any kind, and so using this "trick" you can achieve your wanted numeral-prefixed-name variable without hurting the parser severability.

I would say that technically, naming variables in that manner is not a violation to python guidelines, but it is highly discouraged, and as a rule unnecessary. Using globals for injecting variables is known as a very bad practice and this case should not be an outstanding.


Of course, python could have used an encloser to numerals like strings, say *123*, but I believe the intent of inventing python was to make programming easier, not stretching the limits of variable naming space.


Practically speaking, if you must use number-headed names you better do it with your own dictionary, rather than globals:

>>> number_headed_vars = {'1a': 100} >>> number_headed_vars['1a'] 100 

That way you can create your own variables system - and avoid abusing globals().

like image 157
Uriel Avatar answered Sep 20 '22 12:09

Uriel


This is what you can and can't do with that 1a in globals. You can't really use it in a variable, unless you use all of it's definition in globals (I mean accessing that dictionary), which makes it very uncomfortable for usage (another reason for not doing that).

Basically, 1a is not a real variable as a1 , as shown in the following output:

>>> globals()['1a'] = 1
>>> globals()['1a']
1

>>> a = 1a
File "<stdin>", line 1
    a = 1a
         ^
SyntaxError: invalid syntax

>>> a = globals()['1a']
>>> a
1

>>> globals()['a1'] = 5
>>> a = a1
>>> a
5
like image 41
Ofer Arial Avatar answered Sep 19 '22 12:09

Ofer Arial