[Tutor] Initialize values from a text input file
Tim Johnson
tim at johnsons-web.com
Tue Jan 4 01:13:36 CET 2011
* Steven D'Aprano <steve at pearwood.info> [110103 15:03]:
> Tim Johnson wrote:
>> I'm just have a little fun here, but I bet that comments will help
>> further elighten me on the subtleties of python.
>>
>> consider the following console session:
>>>>> L = ['foo','bar']
>>>>> locals()[L[0]] = L[1]
>
> This will not do what you think it does. In general, local variables are
> not writable except the normal way by `name = value` assignment. Try
> this function:
>
> def spam():
> x = 1
> print(x)
> locals()['x'] = 2
> print(x)
>
>
> The fact that assignment to locals() works in the top-level global scope
> is an accident of implementation.
:) thanks for doing my homework for me.
>
> Have you considered using the ConfigParser module?
Good tip. I will look at it
> (1) This won't work except in the global scope.
>
> (2) Even if it did work, do you trust the source of the text? Taking
> external data provided by arbitrary untrusted users and turning it into
> variables is a good way to have your computer owned by bad guys.
Say what? I'm not talking about anything accessible by "arbitrary
users". Sorry if I gave that impression!
Thanks
--
Tim
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com
More information about the Tutor
mailing list