expanding a variable to a dict

John Machin sjmachin at lexicon.net
Thu Apr 3 19:45:19 EDT 2008


On Apr 4, 9:44 am, Max M <m... at mxm.dk> wrote:
> idle skrev:
>
> > now I'd like to check them all for the existence of certain default
> > keys; ie, if the dicts don't contain the keys, add them in with
> > default values.
>
> > so, I've got:
>
> > for a in ['dictFoo','dictBar','dictFrotz']:
> >     if hasattr(a,'srcdir') == False:
> >         a['srcdir']='/usr/src'
>
> There are a few ways to do it.
>
> for a in ['dictFoo','dictBar','dictFrotz']:

Ummm ... excessive apostrophes plus bonus gross syntax error, dood.
Did you try running any of these snippets???

>      if not a.has_key('srcdir'):

a is a str object. Bang.


>          a['srcdir'] = '/usr/src'
>
> for a in ['dictFoo','dictBar','dictFrotz']:
>      if not 'srcdir' in a:
>          a['srcdir'] = '/usr/src'

a is a str object. Bang.
>
> for a in ['dictFoo','dictBar','dictFrotz']:
>      a.setdefault('srcdir') = '/usr/src'

SyntaxError: can't assign to function call

>
> --
>
> hilsen/regards Max M, Denmark
>
> http://www.mxm.dk/
> IT's Mad Science

Sure is.



More information about the Python-list mailing list