suggestions for improving code fragment please

Tim Chase python.list at tim.thechases.com
Thu Feb 28 17:44:37 EST 2013


On 2013-02-28 16:28, Dave Angel wrote:
> On 02/28/2013 03:37 PM, Tim Chase wrote:
> >    for attr in ("myparm1", "myparm2", "myparm3", ...):
> >      if arglist:
> >        setattr(self, attr, arglist.pop(0))
> >      else:
> >        break
> >
> Or something like (untested):
> 
>      for name, value in zip(["myparm1", "myparm2", "myparm3"],
> arglist): setattr(self, name, value)
>      arglist = []     #if you care how it ends up

The OP's code modified arglist by .pop(0) so I maintained the same
behavior.  This is useful if additional arguments beyond the N named
ones are used for some other purpose and you don't want to figure out
how many were taken. Otherwise, if one wants to keep arglist, Dave's
zip() solution is a cleaner way to go.

-tkc





More information about the Python-list mailing list