[Tutor] Initialize values from a text input file

Tim Johnson tim at johnsons-web.com
Mon Jan 3 23:46:47 CET 2011


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]
>>> foo
'bar'
>>> 'foobar' in locals()
False
>>> 'foo' in locals()
True
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, 'L': ['foo',
'bar'], '__package__': None, '__name__': '__main__', 'foo': 'bar',
'__doc__': None}

Given that I have a text file as input with names and values
with a seperator (TAB, for instance)
I could initialize variables in a local scope, or I could build a
dictionary. Actually the dictionary is preferable for the targeted
application.

However, for the sake of edification, I would welcome comments on
the session above, and let's pretend that `L' is the result of a
line read from a file and split on '\t'.
TIA
tim
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com


More information about the Tutor mailing list