Thank you all for great explanation on this subject. Maybe a few sentences from these conversations could be added to locals() documentation. <br><br>I will make double sure myself while using locals() to end up with valid identifiers. <br>
<br clear="all">Gökhan<br>
<br><br><div class="gmail_quote">On Tue, May 19, 2009 at 1:06 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steven@remove.this.cybersource.com.au">steven@remove.this.cybersource.com.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Sorry for breaking threading, the original post is not being carried by<br>
my ISP.<br>
<div class="im"><br>
<br>
On Tue, 19 May 2009, Gökhan SEVER wrote:<br>
</div><div><div></div><div class="h5">> Hello,<br>
><br>
> Could you please explain why locals() allow me to create variables that<br>
> are not legal in Python syntax. Example: locals()['1abc'] = 55. Calling<br>
> of 1abc results with a syntax error. Shouldn't it be better to raise an<br>
> error during the variable creation time?<br>
<br>
</div></div>No, because it isn't an error to use '1abc' as a dictionary key.<br>
<br>
"locals()['1abc'] = 55" does not create a variable. It creates an object<br>
55, a string '1abc', and uses that string as the key in a dict with 55 as<br>
the value.<br>
<br>
"locals()['abc'] = 55" does not create a variable either. It does exactly<br>
the same thing as above, except that in this case 'abc' happens to be a<br>
valid identifier.<br>
<br>
"abc = 55" also does not create a variable. What it does is exactly the<br>
same as the above, except that the dictionary key is forced to be a valid<br>
identifier by the parser (or perhaps the lexer): the parser won't accept<br>
1abc as a valid identifier, so you can't execute "1abc = 55".<br>
<br>
(Almost... there's actually a slight complication, namely that making<br>
changes to locals() inside a function does not work.)<br>
<br>
Python's programming model is based on namespaces, and namespaces are<br>
implemented as dictionaries: so-called "variables" are key/value pairs<br>
inside a dictionary. Just because a dictionary is used as a namespace<br>
doesn't stop it from being used as a dictionary: you can add any keys/<br>
values which would otherwise be valid. It's still a dictionary, just like<br>
any other dictionary.<br>
<br>
>>> globals()[45] = None<br>
>>> globals()<br>
{'__builtins__': <module '__builtin__' (built-in)>, 45: None, '__name__':<br>
'__main__', '__doc__': None}<br>
<br>
<br>
As for *why* this is done this way, the answer is simplicity of<br>
implementation. Dictionaries don't need to care about what counts as a<br>
valid identifier. Only the lexer/parser needs to care.<br>
<br>
<br>
--<br>
<font color="#888888">Steven<br>
</font><div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>