suggestions for improving code fragment please

Rick Johnson rantingrickjohnson at gmail.com
Thu Feb 28 20:55:49 CET 2013


On Thursday, February 28, 2013 1:47:12 PM UTC-6, The Night Tripper wrote:
>     I'm being very dumb ... how can I simplify this fragment?
> 
>         if arglist:
>             arglist.pop(0)
>             if arglist:
>                 self.myparm1 = arglist.pop(0)
>                 if arglist:
>                     self.myparm2 = arglist.pop(0)
>                     if arglist:
>                         self.myparm3 = arglist.pop(0)
>                         if arglist:
>                             self.parm4 = arglist.pop(0)

Depends. If the length of arglist is "known" you could simply unpack it:

  a,b,c,d = (1,2,3,4)    

If the length is unknown, a while loop would do the trick. All you need is to figure out what "truth condition" to test for on each iteration of the while loop.

 while truthCondition:
    #assign variable to value



More information about the Python-list mailing list