Better writing in python

George Sakkis george.sakkis at gmail.com
Wed Oct 24 11:05:06 EDT 2007


On Oct 24, 10:42 am, cokofree... at gmail.com wrote:
> On Oct 24, 4:15 pm, Paul Hankin <paul.han... at gmail.com> wrote:
>
>
>
> > On Oct 24, 2:02 pm, kyoso... at gmail.com wrote:
>
> > > On Oct 24, 7:09 am, Alexandre Badez <alexandre.ba... at gmail.com> wrote:
>
> > > > I'm just wondering, if I could write a in a "better" way this code
>
> > > > lMandatory = []
> > > > lOptional = []
> > > > for arg in cls.dArguments:
> > > >   if arg is True:
> > > >     lMandatory.append(arg)
> > > >   else:
> > > >     lOptional.append(arg)
> > > > return (lMandatory, lOptional)
>
> > > > I think there is a better way, but I can't see how...
>
> > > You might look into list comprehensions. You could probably do this
> > > with two of them:
>
> > > <code>
> > > # completely untested
> > > lMandatory = [arg for arg in cls.dArguments if arg is True]
> > > lOptional  = [arg for arg in cls.dArguments if arg is False]
> > > </code>
>
> > > Something like that. I'm not the best with list comprehensions, so I
> > > may have the syntax just slightly off.
>
> > Your list comprehensions are right, but 'arg is True' and 'arg is
> > False' are better written as 'arg' and 'not arg' respectively.
>
> > --
> > Paul Hankin
>
> Anyone know why towards arg is True and arg is False, arg is None is
> faster than arg == None ...

And quite often incorrect, especially the "arg is True" and "arg is
False".




More information about the Python-list mailing list