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 s

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

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

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

2009-10-05 Thread Dave Angel
Vern Ceder wrote: 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 pytho

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 saf

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 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 t

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 wrote: > 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 NO

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 wrote: 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

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 wrote: > 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 co

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 wrote: > On Tue, Oct 6, 2009 at 9:58 AM, Dave Angel wrote: >> >> >> >> 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

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/t

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 wrote: > On Tue, Oct 6, 2009 at 9:58 AM, Dave Angel wrote: >> >> >> >> 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 o

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 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

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 wrote: > 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 res

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 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/expr

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,

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 (abb