No-brainer? Dictionary keys to variable name?

Christopher Myers chris.myers at ingenta.com
Fri Aug 2 15:10:16 CEST 2002


Greg Ewing wrote:
> 
> Max M wrote:
> 
> > Christopher Myers wrote:
> >
> >> The reason I'm trying to do this is I have a method as follows:
> >>
> >>     def runSearchTest(self, script_url, f_title="", leftquery="",
> >>                           findwords="titlestext", part="",
> >>                           categories="all", categval=(),
> >>                           topics="all", topicvalue=() ):
> >>
> >> And I've changed my mind about the implementation.  I'd like to change
> >> the parameters to
> >>     def runSearchTest(self, oneTest={}):
> >
> > def runSearchTest(self, **theDict):
> 
> Actually, I think what he wants to do is actually the other
> way around -- he wants to be able to *call* it by passing
> a dict and have the values in it end up in local variables.
> 
> If that's the case, leave the def statement the way
> it is, and call it like this:
> 
>    blarg.runSearchTest(**dictOfArgs)
> 

Excellent.  I knew it was probably a no-brainer.  I had seen this
before, but I was having a senior moment (at 33!).

One question though:  It looks like in order to have the necessary
variables initialized properly, I actually HAVE TO keep my method
definition as is, just in case the dict I pass doesn't contain all the
necessary keys, is that right?

Thanks, all,

-- 
Christopher Myers, Graduate Software Developer 
Ingenta, Inc.
12 Bassett St.
Providence, RI  02903
ph:  401.331.2014 x 102
em:  chris.myers at ingenta.com
aim: chrismyers001



More information about the Python-list mailing list