Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21626 > unrolled thread

Re: Style question (Poll)

Started byTerry Reedy <tjreedy@udel.edu>
First post2012-03-14 17:16 -0400
Last post2012-03-15 10:13 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Style question (Poll) Terry Reedy <tjreedy@udel.edu> - 2012-03-14 17:16 -0400
    Re: Style question (Poll) Jon Clements <joncle@googlemail.com> - 2012-03-15 10:13 -0700
    Re: Style question (Poll) Jon Clements <joncle@googlemail.com> - 2012-03-15 10:13 -0700

#21626 — Re: Style question (Poll)

FromTerry Reedy <tjreedy@udel.edu>
Date2012-03-14 17:16 -0400
SubjectRe: Style question (Poll)
Message-ID<mailman.653.1331759783.3037.python-list@python.org>
On 3/14/2012 4:49 PM, Arnaud Delobelle wrote:
> On 14 March 2012 20:37, Croepha<croepha@gmail.com>  wrote:
>> Which is preferred:
>>
>> for value in list:
>>   if not value is another_value:
>>     value.do_something()
>>     break

Do you really mean 'is' or '=='?

If you mean x is not y, write it that way.
'not x is y' can be misread and misunderstood, depending on whether
the 'is' is true or not.

 >>> not 1 is 1
False
 >>> not (1 is 1)
False
 >>> (not 1) is 1
False

Does not matter how read.

 >>> not (1 is 0)
True
 >>> (not 1) is 0
False
 >>> not 1 is 0
True

Does matter how read.

>> if list and not list[0] is another_value:
>>   list[0].do_something()

Or
try:
   value = mylist[0]
   if value is not another_value: value.dosomething
except IndexError:
   pass

I would not do this in this case of index 0, but if the index were a 
complicated expression or expensive function call, making 'if list' an 
inadequate test, I might.

> Hard to say, since they don't do the same thing :)
>
> I suspect you meant:
>
> for value in list:
>     if not value is another_value:
>         value.do_something()
>     break
>
> I always feel uncomfortable with this because it's misleading: a loop
> that never loops.

I agree. Please do not do this in public ;-).

-- 
Terry Jan Reedy

[toc] | [next] | [standalone]


#21694

FromJon Clements <joncle@googlemail.com>
Date2012-03-15 10:13 -0700
Message-ID<24625945.5378.1331831621256.JavaMail.geo-discussion-forums@vblb5>
In reply to#21626
On Wednesday, 14 March 2012 21:16:05 UTC, Terry Reedy  wrote:
> On 3/14/2012 4:49 PM, Arnaud Delobelle wrote:
> > On 14 March 2012 20:37, Croepha<croepha@gmail.com>  wrote:
> >> Which is preferred:
> >>
> >> for value in list:
> >>   if not value is another_value:
> >>     value.do_something()
> >>     break
> 
> Do you really mean 'is' or '=='?
> 
> If you mean x is not y, write it that way.
> 'not x is y' can be misread and misunderstood, depending on whether
> the 'is' is true or not.
> 
>  >>> not 1 is 1
> False
>  >>> not (1 is 1)
> False
>  >>> (not 1) is 1
> False
> 
> Does not matter how read.
> 
>  >>> not (1 is 0)
> True
>  >>> (not 1) is 0
> False
>  >>> not 1 is 0
> True
> 
> Does matter how read.
> 
> >> if list and not list[0] is another_value:
> >>   list[0].do_something()
> 
> Or
> try:
>    value = mylist[0]
>    if value is not another_value: value.dosomething
> except IndexError:
>    pass
> 
> I would not do this in this case of index 0, but if the index were a 
> complicated expression or expensive function call, making 'if list' an 
> inadequate test, I might.
> 
> > Hard to say, since they don't do the same thing :)
> >
> > I suspect you meant:
> >
> > for value in list:
> >     if not value is another_value:
> >         value.do_something()
> >     break
> >
> > I always feel uncomfortable with this because it's misleading: a loop
> > that never loops.
> 
> I agree. Please do not do this in public ;-).
> 
> -- 
> Terry Jan Reedy

I'm not sure it's efficient or even if I like it, but it avoids try/except and the use of a for loop.

if next( iter(mylist), object() ) is not another_value:
    # ...

Just my 2p,

Jon.

[toc] | [prev] | [next] | [standalone]


#21695

FromJon Clements <joncle@googlemail.com>
Date2012-03-15 10:13 -0700
Message-ID<mailman.688.1331831625.3037.python-list@python.org>
In reply to#21626
On Wednesday, 14 March 2012 21:16:05 UTC, Terry Reedy  wrote:
> On 3/14/2012 4:49 PM, Arnaud Delobelle wrote:
> > On 14 March 2012 20:37, Croepha<croepha@gmail.com>  wrote:
> >> Which is preferred:
> >>
> >> for value in list:
> >>   if not value is another_value:
> >>     value.do_something()
> >>     break
> 
> Do you really mean 'is' or '=='?
> 
> If you mean x is not y, write it that way.
> 'not x is y' can be misread and misunderstood, depending on whether
> the 'is' is true or not.
> 
>  >>> not 1 is 1
> False
>  >>> not (1 is 1)
> False
>  >>> (not 1) is 1
> False
> 
> Does not matter how read.
> 
>  >>> not (1 is 0)
> True
>  >>> (not 1) is 0
> False
>  >>> not 1 is 0
> True
> 
> Does matter how read.
> 
> >> if list and not list[0] is another_value:
> >>   list[0].do_something()
> 
> Or
> try:
>    value = mylist[0]
>    if value is not another_value: value.dosomething
> except IndexError:
>    pass
> 
> I would not do this in this case of index 0, but if the index were a 
> complicated expression or expensive function call, making 'if list' an 
> inadequate test, I might.
> 
> > Hard to say, since they don't do the same thing :)
> >
> > I suspect you meant:
> >
> > for value in list:
> >     if not value is another_value:
> >         value.do_something()
> >     break
> >
> > I always feel uncomfortable with this because it's misleading: a loop
> > that never loops.
> 
> I agree. Please do not do this in public ;-).
> 
> -- 
> Terry Jan Reedy

I'm not sure it's efficient or even if I like it, but it avoids try/except and the use of a for loop.

if next( iter(mylist), object() ) is not another_value:
    # ...

Just my 2p,

Jon.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web