Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: suggestions for improving code fragment please

Reply
Thread Tools

Re: suggestions for improving code fragment please

 
 
Tim Chase
Guest
Posts: n/a
 
      02-28-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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: suggestions for improving code fragment please MRAB Python 0 02-28-2013 09:13 PM
Re: suggestions for improving code fragment please Tim Chase Python 0 02-28-2013 08:37 PM
Re: suggestions for improving code fragment please Ian Kelly Python 0 02-28-2013 07:58 PM
Re: suggestions for improving code fragment please Joel Goldstick Python 0 02-28-2013 07:56 PM
Re: suggestions for improving code fragment please Rick Johnson Python 0 02-28-2013 07:55 PM



Advertisments