Re: Style question (Poll)

2012-03-15 Thread Jon Clements
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, Croephacroe...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Style question (Poll)

2012-03-14 Thread Croepha
Which is preferred:

for value in list:
 if not value is another_value:
   value.do_something()
   break

--or--

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

Comments are welcome, Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Style question (Poll)

2012-03-14 Thread Arnaud Delobelle
On 14 March 2012 20:37, Croepha croe...@gmail.com wrote:
 Which is preferred:

 for value in list:
  if not value is another_value:
    value.do_something()
    break

 --or--

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

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.

-- 
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Style question (Poll)

2012-03-14 Thread Terry Reedy

On 3/14/2012 4:49 PM, Arnaud Delobelle wrote:

On 14 March 2012 20:37, Croephacroe...@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

--
http://mail.python.org/mailman/listinfo/python-list


RE: Style question (Poll)

2012-03-14 Thread Prasad, Ramit
  Which is preferred:
 
  for value in list:
if not value is another_value:
  value.do_something()
  break
 
 Do you really mean 'is' or '=='?

Let me expound on how 'is' and '==' are very different. It may work 
for some comparisons but often not for others. Certain examples work
because of the Python implementation. 

 c = 1
 d = 1
 c is d # This only works because CPython caches small values.
True
 c == d
True
 a = 10
 b = 10
 a is b
False
 a == b
True
 10 is 10 
True

'10 is 10' works because the interpreter caches
the number because it is on the same line.


Only use 'is' if you are looking for objects like True,
False, None or something that MUST be exactly the same object.

In general, use '=='.



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Style question (Poll)

2012-03-14 Thread Arnaud Delobelle
On 14 March 2012 22:15, Prasad, Ramit ramit.pra...@jpmorgan.com wrote:
 Only use 'is' if you are looking for objects like True,
 False, None or something that MUST be exactly the same object.

I've rarely seen valid uses of 'is True' or 'is False'.

-- 
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Style question (Poll)

2012-03-14 Thread Prasad, Ramit
  Only use 'is' if you are looking for objects like True,
  False, None or something that MUST be exactly the same object.
 
 I've rarely seen valid uses of 'is True' or 'is False'.

It can be useful when you think something might be None or False. Although,
I suppose you could always just use 'is None' instead.

 1 == True
True
 1 is True
False
 0 == False
True
 0 is False
False

Granted, the above example is a pretty facetious case; not sure I 
can come up with a reasonably real world use case. 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Style question (Poll)

2012-03-14 Thread Chris Angelico
On Thu, Mar 15, 2012 at 7:37 AM, Croepha croe...@gmail.com wrote:
 Which is preferred:

 for value in list:
  if not value is another_value:
    value.do_something()
    break

 --or--

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

 Comments are welcome, Thanks

General principle: Make the code look like what it's doing. I don't
mean text art and making code in the shape of pi that prints the
digits of pi (although that can be awesome too), but more that a loop
should not be used when you don't intend for it to loop. Consider the
For-Case Paradigm[1] and the amazing confusion value that it can
offer. A loop needn't execute more than once (it needn't even execute
the first time), but it should at least have the _potential_ to
execute the same code multiple times, otherwise it's hardly a loop.

I had a particularly nasty example of a loop-that-wasn't-a-loop at
work a while ago; it was PHP, not Python, so I won't share it here,
but it had a do-while loop and an insidious bug in it. Very tricky.

ChrisA
[1] http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx
-- 
http://mail.python.org/mailman/listinfo/python-list