Re: [Tutor] if n == 0 vs if not n

2009-10-08 Thread Luke Paireepinart
On Tue, Oct 6, 2009 at 2:40 AM, Vern Ceder vce...@canterburyschool.orgwrote:

 Dave Angel wrote:

  Now in this case where it is only used as boolean checks which would be
 the most pythonic way if writing these checks?


  The shorter version may be preferable, but it doesn't generally give the
 same results.  Without knowing the possible data, these substitutions are
 not safe.

 For example, replacing   if not n == 0with if n

 will give different results for values of , []   and so on. It WILL
 work if you know that n is an int or float, however.

 DaveA


 True, I took the OP's statement that they were to be used only as boolean
 checks to mean that there was no type mixing going on. Personally, I would
 say that checking a list or string for equality (or lack thereof) with 0 is
 even less preferable. ;)

 Otherwise, one would at least prefer if n != 0 to if not n == 0, I
 would think.


Actually, I just realized that not has higher precedence than == so this
is really checking if (not n) is equal to 0, not if (n == 0) is (not) True.
 So essentially not n is evaluated and is turned into a bool, which is
then compared to 0, which is the same as False in a boolean context, but n
!= 0 is comparing n to 0, where n may not be the same type (eg. a string).
I'm not sure if there's a situation where this difference matters, but I
feel like there might be.
Anyone have some counter-examples to n != 0 being the same as not n == 0?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-08 Thread Kent Johnson
On Mon, Oct 5, 2009 at 9:13 PM, Luke Paireepinart
rabidpoob...@gmail.com wrote:

 Actually, I just realized that not has higher precedence than == so this
 is really checking if (not n) is equal to 0, not if (n == 0) is (not) True.

No, not is lower precedence than ==. See
http://docs.python.org/reference/expressions.html#summary

 Anyone have some counter-examples to n != 0 being the same as not n == 0?

Well, you can create a pathological class where they are different:

In [17]: class funky(object):
   : def __eq__(self, other):
   : return True
   : def __ne__(self, other):
   : return True

In [18]: f = funky()

In [19]: f != 0
Out[19]: True

In [20]: f == 0
Out[20]: True

In [21]: not f == 0
Out[21]: False


For a less contrived example, if n is a numpy array, n==0 is also a
numpy array but (not n != 0) gives an error:

In [1]: from numpy import *

In [2]: a = array( [ 0, 10, 20, 30, 40 ] )

In [3]: a== 0
Out[3]: array([ True, False, False, False, False], dtype=bool)

In [5]: not a!= 0
---
ValueErrorTraceback (most recent call last)

C:\Project\MangoLib\ipython console in module()

ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-08 Thread wesley chun
 Thanks all for the informative discussion. To re-confirm it was mostly
 for boolean checks like if b == True.


wow, as the OP, you must have been surprised to see how far we have
taken your (seemingly) simple question. we went from boolean checks to
interning! commenting on my previous reply, i was addressing only the
boolean results when shortening the comparisons. as i mentioned
earlier, every Python object has the concept of a boolean value. zeros
and empty containers have False values while all others are True.

however, what i did *not* mention is that these (abbreviated)
comparisons do not work should you care to distinguish between
multiple Python objects/values that have the same boolean value. in
other words, if not b will catch False, None, 0, etc. if your
application is using 3 values like None (for unset value), False
(bad/errror code), and True (correct behavior), then None and False
will both cause the if clause to be executed. in other words, if you
care about the actual objects, then you need to use either == or
is, rather than just checking their boolean outcomes.

now on to the difference between == vs. is and interning. == is
the object *value* comparison operator while is is the object
*identity* comparison operator. although is 0 does work, it's easier
to read and less confusing than == 0. also as others have already
mentioned, it's not a good idea to rely on the undocumented underlying
implementation, and for it to stay consistent. for example, back in
the earlier 2.x releases, the interned integer range was (-1, 101).
when i worked on the 2nd edition of Core Python, i still had that
range in the original manuscript. i then discovered that it changed to
(-5, 256)... no one warned me ahead of time, and i was not paying
enough attention to the python-dev list!

again, the bottom line is to *not* rely on the implementation because
it is always subject to change, granted not for numbers like -1, 0, 1,
but why confuse your fellow Python coder? is is better suited for
constants that are not numbers and that is always only one instance
of: None, True, False.

best regards,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Python Web Development with Django, Addison Wesley, (c) 2009
http://withdjango.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-08 Thread Sander Sweers
On Thu, 2009-10-08 at 12:58 -0700, wesley chun wrote:
 wow, as the OP, you must have been surprised to see how far we have
 taken your (seemingly) simple question.

Pleasently suprised :-) And I am gratefull to see the heavy weights join
in.

 however, what i did *not* mention is that these (abbreviated)
 comparisons do not work should you care to distinguish between
 multiple Python objects/values that have the same boolean value. in
 other words, if not b will catch False, None, 0, etc. if your
 application is using 3 values like None (for unset value), False
 (bad/errror code), and True (correct behavior), then None and False
 will both cause the if clause to be executed. in other words, if you
 care about the actual objects, then you need to use either == or
 is, rather than just checking their boolean outcomes.

Good one, I have to remember this as it will very likely bite me
someday.

I really appriciate all the feedback and good advice!

Thanks
Sander

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-06 Thread Wayne
On Mon, Oct 5, 2009 at 3:37 PM, Sander Sweers sander.swe...@gmail.comwrote:

 Thanks Wesly/Vern for the replies.

 On Mon, 2009-10-05 at 21:56 +0200, Luke Paireepinart wrote:
  if not n == 0
 
  if b == True can be written as if b.
 
 
  However,
  if not n == 0 can be written as if n != 0 but NOT as if n.
  The reason why is that 0 is not equivalent to False even though it
  evaluates to False.
  So
  if not n:
  would be true for n = 0 and for n =  and for n = None
  but
  if n != 0:
  would be true for n =  and n = None but not n = 0.

 Ah, have not thought about this one. In this case it checks the return
 code of a subprocess command which if not zero means build failure. I am
 leaving these as they are because in my opinion if not returncode == 0
 shows clearer what is going on than if returncode.


If it's checking the returncode against a value, Vern makes a good point:

if returncode != 0 makes a whole lot more sense than if not returncode ==
0

Though when dealing with an integer return code, doesn't it make more sense
to use the is operator?

if returncode is 0:
#do something

if returncode is not 0:
#do something

-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-06 Thread Dave Angel

Wayne wrote:

On Mon, Oct 5, 2009 at 3:37 PM, Sander Sweers sander.swe...@gmail.comwrote:

  

Thanks Wesly/Vern for the replies.

On Mon, 2009-10-05 at 21:56 +0200, Luke Paireepinart wrote:


if not n == 0

if b == True can be written as if b.


However,
if not n == 0 can be written as if n != 0 but NOT as if n.
The reason why is that 0 is not equivalent to False even though it
evaluates to False.
So
if not n:
would be true for n = 0 and for n =  and for n = None
but
if n != 0:
would be true for n =  and n = None but not n = 0.
  

Ah, have not thought about this one. In this case it checks the return
code of a subprocess command which if not zero means build failure. I am
leaving these as they are because in my opinion if not returncode == 0
shows clearer what is going on than if returncode.




If it's checking the returncode against a value, Vern makes a good point:

if returncode != 0 makes a whole lot more sense than if not returncode ==
0

Though when dealing with an integer return code, doesn't it make more sense
to use the is operator?

if returncode is 0:
#do something

if returncode is not 0:
#do something

-Wayne

  
No, because you're not assured that all integers that are equal are the 
same object.  Python optimizes that for small integers, but there's no 
documented range that you can count on it.


 x = 374
 y = 374
 print id(x), id(y)
12721272 12721296
 x==y
True
 x is y
False

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-06 Thread Wayne
On Tue, Oct 6, 2009 at 9:58 AM, Dave Angel da...@ieee.org wrote:

 snip

No, because you're not assured that all integers that are equal are the same
 object.  Python optimizes that for small integers, but there's no documented
 range that you can count on it.


But for this specific case - checking a return code against zero, should it
still be considered unreliable? The only case that you are looking for
correctness is 0 == 0, any other case should evaluate as false, so I guess
the question is does python always optimize for zero? Any other optimization
is irrelevant, AFAIK.

-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-06 Thread Andre Engels
On Tue, Oct 6, 2009 at 5:08 PM, Wayne sri...@gmail.com wrote:
 On Tue, Oct 6, 2009 at 9:58 AM, Dave Angel da...@ieee.org wrote:

 snip

 No, because you're not assured that all integers that are equal are the
 same object.  Python optimizes that for small integers, but there's no
 documented range that you can count on it.


 But for this specific case - checking a return code against zero, should it
 still be considered unreliable? The only case that you are looking for
 correctness is 0 == 0, any other case should evaluate as false, so I guess
 the question is does python always optimize for zero? Any other optimization
 is irrelevant, AFAIK.

Never rely on optimizations like this being done, or on them not being
done. The only save way to code is having your code work in both
cases.


-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-06 Thread Sander Sweers
Thanks all for the informative discussion. To re-confirm it was mostly
for boolean checks like if b == True.

Greets
Sander

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-06 Thread Kent Johnson
On Tue, Oct 6, 2009 at 11:08 AM, Wayne sri...@gmail.com wrote:
 On Tue, Oct 6, 2009 at 9:58 AM, Dave Angel da...@ieee.org wrote:

 snip

 No, because you're not assured that all integers that are equal are the
 same object.  Python optimizes that for small integers, but there's no
 documented range that you can count on it.


 But for this specific case - checking a return code against zero, should it
 still be considered unreliable?

Yes. It is undocumented, implementation-specific behaviour and you
should not depend on it without good reason. In this case, there is no
reason at all to prefer if x is 0 to if x == 0.

 The only case that you are looking for
 correctness is 0 == 0, any other case should evaluate as false, so I guess
 the question is does python always optimize for zero? Any other optimization
 is irrelevant, AFAIK.

Define always. In all past and future versions of every
implementation of Python? Unlikely and unknowable. In all current
versions of CPython? Yes, AFAIK.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-06 Thread Kent Johnson
On Tue, Oct 6, 2009 at 9:04 AM, Wayne sri...@gmail.com wrote:

 If it's checking the returncode against a value, Vern makes a good point:
 if returncode != 0 makes a whole lot more sense than if not returncode ==
 0
 Though when dealing with an integer return code, doesn't it make more sense
 to use the is operator?
 if returncode is 0:
     #do something
 if returncode is not 0:
     #do something

No! Equality and identity are different. CPython does cache small
integers but that is an implementation detail that you should not rely
on. For large values you cannot presume that equal numbers are also
identical. For example:

In [1]: a=10

In [2]: b=10

In [3]: a==b
Out[3]: True

In [4]: a is b
Out[4]: False

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Vern Ceder

Hi Sander,

PEP 8, the Style Guide for Python Code
http://www.python.org/dev/peps/pep-0008/ is pretty clear that the
shorter version is preferable:

if s:

if n:

if b:

if not b:

and so on...

Cheers,
Vern

Sander Sweers wrote:

Hi Tutors,

I am going through someone's python script and I am seeing a lot of the
following boolean checks.

if not s == 

if not n == 0

if b == True

if not b == True

etc..

All of these can be written without the == notation like if n, if s
etc.

Now in this case where it is only used as boolean checks which would be
the most pythonic way if writing these checks?

Thanks
Sander

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


--
This time for sure!
   -Bullwinkle J. Moose
-
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Sander Sweers
Thanks Wesly/Vern for the replies.

On Mon, 2009-10-05 at 21:56 +0200, Luke Paireepinart wrote: 
 if not n == 0
 
 if b == True can be written as if b.
 
 
 However,
 if not n == 0 can be written as if n != 0 but NOT as if n.
 The reason why is that 0 is not equivalent to False even though it
 evaluates to False.
 So 
 if not n:
 would be true for n = 0 and for n =  and for n = None
 but
 if n != 0:
 would be true for n =  and n = None but not n = 0.

Ah, have not thought about this one. In this case it checks the return
code of a subprocess command which if not zero means build failure. I am
leaving these as they are because in my opinion if not returncode == 0
shows clearer what is going on than if returncode.

 Whoever wrote your code probably thinks he knows the types of the
 variables beforehand so he's just making assumptions as to whether a
 variable is an int / string / etc. So you can probably safely assume
 that they're boolean checks, but I take no responsibility if you break
 something :)

When I make the changes and send in a patch it will be reviewed. So any
siliness I come up with will be shot down quickly ;-)

Thanks
Sander 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread wesley chun
 I am going through someone's python script and I am seeing a lot of the
 following boolean checks.

 if not s == 
 if not n == 0
 if b == True
 if not b == True
 etc..

 All of these can be written without the == notation like if n, if s
 etc.Now in this case where it is only used as boolean checks which would be
 the most pythonic way if writing these checks?


it would be the same as what you have already described. checking
against Boolean literals follows the same logic, i.e., if b, if not
b, etc. of course, the reasoning behind what you said and my
suggestion is that all Python objects evaluate to some sort of Boolean
value. the == 0 and '== ' (and their corresponding nots) aren't
necessary because both 0 and  have a Boolean False value, as does
False.

the general rule is that any numeric zero (0, 0.0, 0.0+0.0J, etc.) or
empty container (i.e., str, list, tuple, dict, set, etc.), are all
False. all other values are True.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
Python Fundamentals, Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Dave Angel

Vern Ceder wrote:

div class=moz-text-flowed style=font-family: -moz-fixedHi Sander,

PEP 8, the Style Guide for Python Code
http://www.python.org/dev/peps/pep-0008/ is pretty clear that the
shorter version is preferable:

if s:

if n:

if b:

if not b:

and so on...

Cheers,
Vern

Sander Sweers wrote:

Hi Tutors,

I am going through someone's python script and I am seeing a lot of the
following boolean checks.

if not s == 

if not n == 0

if b == True

if not b == True

etc..

All of these can be written without the == notation like if n, if s
etc.

Now in this case where it is only used as boolean checks which would be
the most pythonic way if writing these checks?

Thanks
Sander

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


The shorter version may be preferable, but it doesn't generally give the 
same results.  Without knowing the possible data, these substitutions 
are not safe.


For example, replacing   if not n == 0with if n

will give different results for values of , []   and so on. It 
WILL work if you know that n is an int or float, however.


DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Vern Ceder

Dave Angel wrote:


Now in this case where it is only used as boolean checks which would be
the most pythonic way if writing these checks?



The shorter version may be preferable, but it doesn't generally give the 
same results.  Without knowing the possible data, these substitutions 
are not safe.


For example, replacing   if not n == 0with if n

will give different results for values of , []   and so on. It 
WILL work if you know that n is an int or float, however.


DaveA


True, I took the OP's statement that they were to be used only as 
boolean checks to mean that there was no type mixing going on. 
Personally, I would say that checking a list or string for equality (or 
lack thereof) with 0 is even less preferable. ;)


Otherwise, one would at least prefer if n != 0 to if not n == 0, I 
would think.


Cheers, Vern
--
This time for sure!
   -Bullwinkle J. Moose
-
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Luke Paireepinart
On Mon, Oct 5, 2009 at 9:28 PM, Sander Sweers sander.swe...@gmail.comwrote:


 Hi Tutors,

 I am going through someone's python script and I am seeing a lot of the
 following boolean checks.

 if not s == 

 if not n == 0

 if b == True

 if not b == True

 etc..

 All of these can be written without the == notation like if n, if s


No, they cannot.  Some of them can be, others cannot.
if b == True can be written as if b.

However,
if not n == 0 can be written as if n != 0 but NOT as if n.
The reason why is that 0 is not equivalent to False even though it evaluates
to False.
So
if not n:
would be true for n = 0 and for n =  and for n = None
but
if n != 0:
would be true for n =  and n = None but not n = 0.

The same is true for
if not s == 

 Now in this case where it is only used as boolean checks which would be
 the most pythonic way if writing these checks?

If you're sure they're boolean checks, if n: or if not n: is usually how
I see it written.
Whoever wrote your code probably thinks he knows the types of the variables
beforehand so he's just making assumptions as to whether a variable is an
int / string / etc. So you can probably safely assume that they're boolean
checks, but I take no responsibility if you break something :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor