suggestions for improving code fragment please

MRAB python at mrabarnett.plus.com
Thu Feb 28 22:13:40 CET 2013


On 2013-02-28 19:47, The Night Tripper wrote:
> Hi there
>      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)
>          # ...
>
You could just catch the exception:

     try:
         arglist.pop(0)
         self.myparm1 = arglist.pop(0)
         self.myparm2 = arglist.pop(0)
         self.myparm3 = arglist.pop(0)
         self.parm4 = arglist.pop(0)
     except IndexError:
         pass




More information about the Python-list mailing list