bvidinli <bvidinli@gmail .comwrites:
Especially when you don't have to do this.
At the interactive prompt, type 'help(dict)' and read carefully about
the different methods provided by dictionary objects (one called 'get'
may be of particular interest to you).
HTH
--
Arnaud
i use dictionaries to hold some config data,
such as:
>
conf={'key1':'v alue1','key2':' value2'}
and so on...
>
when i try to process conf, i have to code every time like:
if conf.has_key('k ey1'):
if conf['key1']<>'':
other commands....
>
>
this is very annoying.
such as:
>
conf={'key1':'v alue1','key2':' value2'}
and so on...
>
when i try to process conf, i have to code every time like:
if conf.has_key('k ey1'):
if conf['key1']<>'':
other commands....
>
>
this is very annoying.
in php, i was able to code only like:
if conf['key1']=='someth'
>
in python, this fails, because, if key1 does not exists, it raises
an exception.
>
MY question: is there a way to directly get value of an
array/tuple/dict item by key, as in php above, even if key may not
exist, i should not check if key exist, i should only use it, if it
does not exist, it may return only empty, just as in php....
if conf['key1']=='someth'
>
in python, this fails, because, if key1 does not exists, it raises
an exception.
>
MY question: is there a way to directly get value of an
array/tuple/dict item by key, as in php above, even if key may not
exist, i should not check if key exist, i should only use it, if it
does not exist, it may return only empty, just as in php....
the different methods provided by dictionary objects (one called 'get'
may be of particular interest to you).
HTH
--
Arnaud