Re: suggestions for improving code fragment please

Discussion in 'Python' started by Tim Chase, Feb 28, 2013.

  1. Tim Chase

    Tim Chase Guest

    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
    Tim Chase, Feb 28, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Johnson
    Replies:
    0
    Views:
    90
    Rick Johnson
    Feb 28, 2013
  2. Joel Goldstick
    Replies:
    0
    Views:
    99
    Joel Goldstick
    Feb 28, 2013
  3. Ian Kelly
    Replies:
    0
    Views:
    89
    Ian Kelly
    Feb 28, 2013
  4. Tim Chase
    Replies:
    0
    Views:
    88
    Tim Chase
    Feb 28, 2013
  5. MRAB
    Replies:
    0
    Views:
    85
Loading...

Share This Page