Re: checking if two things do not equal None

2014-03-31 Thread Jeremy Sanders
contact.tri...@gmail.com wrote:

 if (a, b) != (None, None):
 or
 if a != None != b:
 
 Preference? Pros? Cons? Alternatives?

I couldn't see anyone else give this, but I like

if None not in (a, b):
 pass

Jeremy


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


Re: checking if two things do not equal None

2014-03-31 Thread Abe
 I couldn't see anyone else give this, but I like
 if None not in (a, b):

I did.

 I am now considering:

 if None not in (a,b):
 or
 if (a is not None) and (b is not None):

However, I decided to just turn the two parameters into one (sequence), since 
they were logically grouped anyhow.


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


Re: checking if two things do not equal None

2014-03-31 Thread Moritz Emanuel Beber


On 31/03/14 19:28, Abe wrote:

I couldn't see anyone else give this, but I like
if None not in (a, b):

I did.


I am now considering:
if None not in (a,b):
or
if (a is not None) and (b is not None):

That's just

if not (a is None or b is None):

but you seem to have found your way.

However, I decided to just turn the two parameters into one (sequence), since 
they were logically grouped anyhow.




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


Re: checking if two things do not equal None

2014-03-30 Thread Steven D'Aprano
On Sat, 29 Mar 2014 19:54:09 -0700, Rustom Mody wrote:

 On Sunday, March 30, 2014 8:09:45 AM UTC+5:30, Roy Smith wrote:
 I have no particular problem with
 
  x  2  y
 
 because it fits the same pattern.  But, if you show me
 
  a != None != b:
 
 my brain just goes into overload.  Honestly, I don't even know what
 that means.  My brain keeps trying to stick a, None, and b on Mrs.
 Albaum's number line and keeps walking into the wall.  If you (the
 editorial you) tell me that my failure to grok that expression means
 I'm not fluent in Python, well then, guilty as charged.
 
 Math Terminology
[...]
 So for != chained comparisons are not natural (or IMHO appropriate)

I tend to agree they're not natural, although appropriate is another 
thing. The problem is that we tend to read something like:

a != b != c

as all of a, b and c are unequal, corresponding to:

a == b == c

as all of a, b and c are equal. But that's not what it means. It means 
that a != b and b != c, but it says nothing about a and c. And that was 
my mistake. The OP actually got it right in their first post, but 
sticking None in the middle to ensure it partakes of both comparisons.

a is not None is not b

Still, that's not easily extended to a third item, this would be wrong:

a is not None is not b is not c

since c only gets compared against b, not None. Better is to factor the 
not out:

not (a is b is c is None)


which now should be clear: you're testing whether or not *all* of a, b 
and c are None. If you prefer:

not all(x is None for x in (a, b, c))


Which is more readable is a matter of personal preference.

I think Johannes got it right: boolean logic is easier to reason about 
when there is a minimum of nots.


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-30 Thread Chris Angelico
On Sun, Mar 30, 2014 at 4:54 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 On Sun, 30 Mar 2014 13:15:18 +1100, Chris Angelico wrote:

 Chained comparisons where you're checking a single variable against two
 constants make perfect sense:

 2  x  5

 Chained comparisons where you check a single constant against two
 variables don't, so much:

 x  2  y

 What exactly does that mean, and why is it written that way?

 It checks that 2 is strictly bounded between x on the left and y on the
 right, i.e. that 2 is inside the open interval x...y. I don't know why
 you think that's unclear. But then I do have a maths background and I'm
 used to chaining comparisons.

 Write it like this:

 low = x
 high = y
 a = 2

 low  a  high

 Does that make more sense? Well-chosen names are good. The fact that a is
 a constant rather than a variable is no big deal:

 low  2  high

The problem isn't that I can't see what the comparisons are. It makes
very good sense to bound a variable within constants; but you already
know exactly where 2 is on the number line, so asking Is 2 between
these two variables seems a bit odd. Maybe it's less so with the
strong mathematical background, but it seems odd to me.

 It'd be more useful but less clear if one of the conditions points the
 other way:

 x  2  y

 which checks that they're both less than two,

 which is quite different from what you wrote the first time.


 but IMO in a less-than-clear way.

 That's an understatement. If I saw code chaining comparisons in that
 fashion, I would assume the second operator  was a typo.

 Chaining less-than and greater than operators should, for clarity, always
 be written in a single order. E.g. a = b  c  d, not a = b  d  c.

 (The second contains a subtle bug too.)

Agreed.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-30 Thread Gregory Ewing

Roy Smith wrote:

But, if you show me

 a != None != b:

my brain just goes into overload.


Chained comparisons get weird with not-equal operators.
If you see

  a == b == c

then it implies that a == c, but

  a != b != c

does *not* imply that a != c. At least it doesn't in
Python; I've never seen any mathematicians write that, so
I don't know what they would make of it.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-30 Thread Ben Finney
Chris Angelico ros...@gmail.com writes:

 The problem isn't that I can't see what the comparisons are. It makes
 very good sense to bound a variable within constants; but you already
 know exactly where 2 is on the number line, so asking Is 2 between
 these two variables seems a bit odd. Maybe it's less so with the
 strong mathematical background, but it seems odd to me.

I don't feel odd about asking the question “Is 2 between these two
values?”. It's straightforward and concise. Can you explain better why
you find it odd?

-- 
 \ “You are welcome to visit the cemetery where famous Russian and |
  `\Soviet composers, artists, and writers are buried daily except |
_o__)   Thursday.” —Russian orthodox monastery, Moscow |
Ben Finney

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


Re: checking if two things do not equal None

2014-03-30 Thread Chris Angelico
On Sun, Mar 30, 2014 at 5:52 PM, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Chris Angelico ros...@gmail.com writes:

 The problem isn't that I can't see what the comparisons are. It makes
 very good sense to bound a variable within constants; but you already
 know exactly where 2 is on the number line, so asking Is 2 between
 these two variables seems a bit odd. Maybe it's less so with the
 strong mathematical background, but it seems odd to me.

 I don't feel odd about asking the question “Is 2 between these two
 values?”. It's straightforward and concise. Can you explain better why
 you find it odd?

Possibly because the variable between two constants is something
I've done often (usually in the more explicit form of x  min  x 
max in a language without chained comparisons), usually
bounds-checking some value. I've never had to ask whether a single
constant has two variables, one on either side. But that's just that
I've personally never done it; it doesn't mean nobody does it, by any
means.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-30 Thread Marko Rauhamaa
Gregory Ewing greg.ew...@canterbury.ac.nz:

   a != b != c

 does *not* imply that a != c. At least it doesn't in Python; I've
 never seen any mathematicians write that, so I don't know what they
 would make of it.

Any resemblance between mathematics notation and Python is purely
coincidental. I must admit I had missed Python's chained comparisons
until this discussion, but now I looked up the definition:

comparison::=  or_expr ( comp_operator or_expr )*
comp_operator ::=   |  | == | = | = | !=
   | is [not] | [not] in

[...]

Formally, if a, b, c, ..., y, z are expressions and op1, op2, ...,
opN are comparison operators, then a op1 b op2 c ... y opN z is
equivalent to a op1 b and b op2 c and ... y opN z, except that each
expression is evaluated at most once.


That means, in my opinion, that you should feel free to use chaining any
way you see fit. Also, the rule is crystal-clear and easy to grasp:
there's an implicit and there.

It's another thing, then, if it was a good idea to include chaining
there in the first place, but I trust the idea was properly vetted and
double checked against possible parsing ambiguities.

Even without chaining is not is a bit suspect:

 False is not 0
True
 False is (not 0)
False
 False is not not not 0
  File stdin, line 1
False is not not not 0
   ^
SyntaxError: invalid syntax



Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-30 Thread Roy Smith
In article 5337b4e4$0$29994$c3e8da3$54964...@news.astraweb.com,
 Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:

 I think Johannes got it right: boolean logic is easier to reason about 
 when there is a minimum of nots.

I used to do a lot of digital logic design.  In certain logic families, 
it's easier to build a NAND gate than an AND gate (and similarly, NOR is 
easier than OR).  This leads to lots of inverted logic.  Adding to the 
confusion, many designs would use active low logic, which means a 1 
was represented by a low voltage, and a 0 by a high voltage.  So, you 
quickly end up with gibberish like, not active low clear nand not 
active low enable clock.  I'm glad I don't do that stuff any more.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-30 Thread MRAB

On 2014-03-30 13:21, Roy Smith wrote:

In article 5337b4e4$0$29994$c3e8da3$54964...@news.astraweb.com,
  Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:


I think Johannes got it right: boolean logic is easier to reason about
when there is a minimum of nots.


I used to do a lot of digital logic design.  In certain logic families,
it's easier to build a NAND gate than an AND gate (and similarly, NOR is
easier than OR).  This leads to lots of inverted logic.  Adding to the
confusion, many designs would use active low logic, which means a 1
was represented by a low voltage, and a 0 by a high voltage.  So, you
quickly end up with gibberish like, not active low clear nand not
active low enable clock.  I'm glad I don't do that stuff any more.


When you're building with discrete logic chips, NAND gates are useful
because you can use them as inverters too, which helps to keep the chip
count down.
--
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-30 Thread Gregory Ewing

Roy Smith wrote:
Adding to the 
confusion, many designs would use active low logic, which means a 1 
was represented by a low voltage, and a 0 by a high voltage.  So, you 
quickly end up with gibberish like, not active low clear nand not 
active low enable clock.


There are ways of dealing with that in schematic diagrams.
For exammple, if you have two active-low signals A and B
and want to express A is active or B is active, you
draw an OR gate symbol with inversion circles on the
inputs. That's equivalent to a NAND gate, but makes the
intention clear.

Schematics drawn that way are much easier to follow than
ones that only use the inverted-output versions of the
symbols.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Steven D'Aprano
On Sat, 29 Mar 2014 11:56:50 -0700, contact.trigon wrote:

 if (a, b) != (None, None):
 or
 if a != None != b:
 
 Preference? Pros? Cons? Alternatives?

Do you actually want to check for arbitrary objects which may claim to 
equal None, or do you want to check for objects which are None?


Nearly always when people test for == to None, they don't really mean it. 
They actually want to use an identity test. I'm going to assume the same 
holds here.

if not (a is b is None): ...


Or if you prefer:

if a is not b is not None: ...




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Lele Gaifax
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes:

 if not (a is b is None): ...

 Or if you prefer:

 if a is not b is not None: ...

 1 is not 1 is not None
False

So definitely the former!

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: checking if two things do not equal None

2014-03-29 Thread contact . trigon
 Do you actually want to check for arbitrary objects which may claim to  
 equal None, or do you want to check for objects which are None?

Arbitrary objects are not a concern.

 if not (a is b is None): ...
 
 if a is not b is not None: ...

Thanks for the examples.

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


Re: checking if two things do not equal None

2014-03-29 Thread Johannes Bauer
On 29.03.2014 20:05, Steven D'Aprano wrote:
 On Sat, 29 Mar 2014 11:56:50 -0700, contact.trigon wrote:
 
 if (a, b) != (None, None):
 or
 if a != None != b:

 Preference? Pros? Cons? Alternatives?

 if not (a is b is None): ...
 
 Or if you prefer:
 
 if a is not b is not None: ...

Is this an obfuscated coding contest? Why do you opt for a solution that
one has to at least think 2 seconds about when the simplest solution:

if (a is not None) or (b is not None):

is immediately understandable by everyone?

Cheers,
Johannes

-- 
 Wo hattest Du das Beben nochmal GENAU vorhergesagt?
 Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa hidbv3$om2$1...@speranza.aioe.org
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Roy Smith
In article lh7cb4$ntu$2...@news.albasani.net,
 Johannes Bauer dfnsonfsdu...@gmx.de wrote:

 On 29.03.2014 20:05, Steven D'Aprano wrote:
  On Sat, 29 Mar 2014 11:56:50 -0700, contact.trigon wrote:
  
  if (a, b) != (None, None):
  or
  if a != None != b:
 
  Preference? Pros? Cons? Alternatives?
 
  if not (a is b is None): ...
  
  Or if you prefer:
  
  if a is not b is not None: ...
 
 Is this an obfuscated coding contest? Why do you opt for a solution that
 one has to at least think 2 seconds about when the simplest solution:
 
 if (a is not None) or (b is not None):
 
 is immediately understandable by everyone?

I agree with that.  But

 if (a, b) != (None, None):

seems pretty straight-forward to me too.  In fact, if anything, it seems 
easier to understand than

 if (a is not None) or (b is not None):

I certainly agree that things like

 if a is not b is not None: ...

belong in an obfuscated coding contest.  Code gets read a lot more often 
than it get written.  Make it dead-ass simple to understand, and future 
generations of programmers who inherit your code will thank you for it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Dave Angel
 Roy Smith r...@panix.com Wrote in message:
 In article lh7cb4$ntu$2...@news.albasani.net,
  Johannes Bauer dfnsonfsdu...@gmx.de wrote:
 
 On 29.03.2014 20:05, Steven D'Aprano wrote:
  On Sat, 29 Mar 2014 11:56:50 -0700, contact.trigon wrote:
  
  if (a, b) != (None, None):
  or
  if a != None != b:
 
  Preference? Pros? Cons? Alternatives?
 
  if not (a is b is None): ...
  
  Or if you prefer:
  
  if a is not b is not None: ...
 
 Is this an obfuscated coding contest? Why do you opt for a solution that
 one has to at least think 2 seconds about when the simplest solution:
 
 if (a is not None) or (b is not None):
 
 is immediately understandable by everyone?
 
 I agree with that.  But
 
 if (a, b) != (None, None):
 
 seems pretty straight-forward to me too.  In fact, if anything, it seems 
 easier to understand than
 
 if (a is not None) or (b is not None):
 
 I certainly agree that things like
 
 if a is not b is not None: ...
 
 belong in an obfuscated coding contest.  Code gets read a lot more often 
 than it get written.  Make it dead-ass simple to understand, and future 
 generations of programmers who inherit your code will thank you for it.
 

The other advantage to keeping it simple is it's more than likely
 to be right.  If we take the original form as the spec, we'll
 find that two of the alternatives are not even equivalent.
 

def trigon1(a, b):
return (a,b) != (None, None)  #master

def trigon2(a, b):
return a != None != b.  #different

def steven1(a, b):
return not(a is b is None)

def steven2(a, b):
return a is not b is not None  #different

def johannes(a, b):
return (a is not None) or (b is not None)

table = [
trigon1,
trigon2,
steven1,
steven2,
johannes
]

for func in table:
print func.__name__
print func(None, None), func(None, 42), func(42, None),
 func(42, 42), func(42, never)




-- 
DaveA

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


Re: checking if two things do not equal None

2014-03-29 Thread Johannes Bauer
On 29.03.2014 22:07, Roy Smith wrote:

 I agree with that.  But
 
 if (a, b) != (None, None):
 
 seems pretty straight-forward to me too.  In fact, if anything, it seems 
 easier to understand than
 
 if (a is not None) or (b is not None):

Yes, probably. I liked the original, too. If I were writing the code,
I'd probably try to aim to invert the condition though and simply do

if (a is None) and (b is None)

Which is pretty easy to understand for even a rookie programmer.

 I certainly agree that things like
 
 if a is not b is not None: ...
 
 belong in an obfuscated coding contest.  Code gets read a lot more often 
 than it get written.  Make it dead-ass simple to understand, and future 
 generations of programmers who inherit your code will thank you for it.

Absolutely.

Cheers,
Johannes

-- 
 Wo hattest Du das Beben nochmal GENAU vorhergesagt?
 Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa hidbv3$om2$1...@speranza.aioe.org
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Johannes Bauer
On 29.03.2014 22:55, Johannes Bauer wrote:

 if (a is not None) or (b is not None):
 
 Yes, probably. I liked the original, too. If I were writing the code,
 I'd probably try to aim to invert the condition though and simply do
 
 if (a is None) and (b is None)
 
 Which is pretty easy to understand for even a rookie programmer.

Let me expand on that thought one or two more sentences: Although it may
seem really trivial, inversions (not) in my opinion can really make
code unreadable. One thing that I regularly see when peer-reviewing code
is something like:

if not feature_disabled:

or one that I've seen in-field (modulo the programming language and the
variable names):

if (not no_delayed_commit) and (not data_unchanged):

instead of:

if immediate_commit and data_changed:

Enough of my two cents for today :-)
Cheers,
Johannes


-- 
 Wo hattest Du das Beben nochmal GENAU vorhergesagt?
 Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa hidbv3$om2$1...@speranza.aioe.org
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Tim Chase
On 2014-03-29 17:07, Roy Smith wrote:
  if (a is not None) or (b is not None):
  
  is immediately understandable by everyone?  
 
 I agree with that.  But
 
  if (a, b) != (None, None):  
 
 seems pretty straight-forward to me too.  In fact, if anything, it
 seems easier to understand than

And for cases where you have more than one or two things to test for
None-itude, you could use

  if all(x is None for x in [a, b, c, d]):
do_something_if_theyre_all_None()

or

  if all(x is not None for x in [a, b, c, d]):
do_something_if_no_Nones()

or

  if not any(x is None for x in [a, b, c, d]):
do_something_if_no_Nones()

which I find *much* more readable from a maintenance point of view.

-tkc



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


Re: checking if two things do not equal None

2014-03-29 Thread Roy Smith
On Mar 29, 2014, at 6:36 PM, Tim Chase wrote:

 And for cases where you have more than one or two things to test for
 None-itude, you could use
 
  if all(x is None for x in [a, b, c, d]):
do_something_if_theyre_all_None()

I might have written that as:

if set([a, b, c, d]) == set(None)

That's even clearer if you happen to already have the items in an iterable:

if set(conditions) == set(None)


--
Roy Smith
r...@panix.com



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


Re: checking if two things do not equal None

2014-03-29 Thread Tim Chase
On 2014-03-29 18:41, Roy Smith wrote:
 On Mar 29, 2014, at 6:36 PM, Tim Chase wrote:
 
  And for cases where you have more than one or two things to test
  for None-itude, you could use
  
   if all(x is None for x in [a, b, c, d]):
 do_something_if_theyre_all_None()
 
 I might have written that as:
 
 if set([a, b, c, d]) == set(None)
 
 That's even clearer if you happen to already have the items in an
 iterable:
 
 if set(conditions) == set(None)

Though am I correct that your iteration tests for equality, while
mine tests for identity?  Also, my version bails early in the event
quitting early is possible.  That's particularly useful in the case
of doing something like

  if all(x() is None for x in [func1, func2, func3, costly_func]):
do_something()

-tkc



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


Re: checking if two things do not equal None

2014-03-29 Thread Roy Smith
In article mailman.8703.1396133206.18130.python-l...@python.org,
 Tim Chase t...@thechases.com wrote:

 On 2014-03-29 18:41, Roy Smith wrote:
  On Mar 29, 2014, at 6:36 PM, Tim Chase wrote:
  
   And for cases where you have more than one or two things to test
   for None-itude, you could use
   
if all(x is None for x in [a, b, c, d]):
  do_something_if_theyre_all_None()
  
  I might have written that as:
  
  if set([a, b, c, d]) == set(None)
  
  That's even clearer if you happen to already have the items in an
  iterable:
  
  if set(conditions) == set(None)
 
 Though am I correct that your iteration tests for equality, while
 mine tests for identity?

Hmmm, you're almost certainly correct on that, but you would have to 
have a perversely designed class for that to make a difference.  I'll 
take the increased readability.

 Also, my version bails early in the event
 quitting early is possible.  That's particularly useful in the case
 of doing something like
 
   if all(x() is None for x in [func1, func2, func3, costly_func]):
 do_something()

Again, you're correct.  But, I'll take the increased readability over 
the premature optimization :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Terry Reedy

On 3/29/2014 2:56 PM, contact.tri...@gmail.com wrote:

if (a, b) != (None, None):
or
if a != None != b:

Preference? Pros? Cons? Alternatives?


if a is not None is not b
==
if a is not None and None is not b
==
if a is not None and b is not None
which is what I would write if not trying to be cute.
a  x  b is more readable as a chained comparison than the double is not.

--
Terry Jan Reedy

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


Re: checking if two things do not equal None

2014-03-29 Thread Chris Angelico
On Sun, Mar 30, 2014 at 9:46 AM, Tim Chase t...@thechases.com wrote:
 Though am I correct that your iteration tests for equality, while
 mine tests for identity?  Also, my version bails early in the event
 quitting early is possible.  That's particularly useful in the case
 of doing something like

   if all(x() is None for x in [func1, func2, func3, costly_func]):
 do_something()

Presumably you mean to actually call those functions, as checking the
identity of a costly function is still cheap :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread contact . trigon
Thanks everyone; it has been very educational.

 Dave Angel:
 ...we'll find that two of the alternatives are not even equivalent.

That helped me realize (a,b) != (None, None) is not correct for the function.

It's a case where two parameters have None as the default argument. What I want 
is to make sure that both are not None. I am now considering:

if None not in (a,b):
or
if (a is not None) and (b is not None):
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Ethan Furman

On 03/29/2014 02:01 PM, Johannes Bauer wrote:

On 29.03.2014 20:05, Steven D'Aprano wrote:

On Sat, 29 Mar 2014 11:56:50 -0700, contact.trigon wrote:


if (a, b) != (None, None):
or
if a != None != b:

Preference? Pros? Cons? Alternatives?


if not (a is b is None): ...

Or if you prefer:

if a is not b is not None: ...


Is this an obfuscated coding contest? Why do you opt for a solution that
one has to at least think 2 seconds about when the simplest solution:

if (a is not None) or (b is not None):

is immediately understandable by everyone?


+1

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Tim Chase
On 2014-03-30 10:17, Chris Angelico wrote:
 On Sun, Mar 30, 2014 at 9:46 AM, Tim Chase t...@thechases.com
 wrote:
 Though am I correct that your iteration tests for equality, while
 mine tests for identity?  Also, my version bails early in the
 event quitting early is possible.  That's particularly useful in
 the case of doing something like

   if all(x() is None for x in [func1, func2, func3, costly_func]):
^^^
 do_something()
 
 Presumably you mean to actually call those functions, as checking
 the identity of a costly function is still cheap :)

Which is what I do...calling only those necessary until the all/any
condition has been met. :-)

If you create the list of things to iterate over by calling them as
you create the list, then you don't save much of anything.  If you
only call until one of them breaks the any/all construct, you save
all the subsequent function calls.

-tkc




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


Re: checking if two things do not equal None

2014-03-29 Thread Chris Angelico
On Sun, Mar 30, 2014 at 12:19 PM, Tim Chase
python.l...@tim.thechases.com wrote:
 On 2014-03-30 10:17, Chris Angelico wrote:
 On Sun, Mar 30, 2014 at 9:46 AM, Tim Chase t...@thechases.com
 wrote:
 Though am I correct that your iteration tests for equality, while
 mine tests for identity?  Also, my version bails early in the
 event quitting early is possible.  That's particularly useful in
 the case of doing something like

   if all(x() is None for x in [func1, func2, func3, costly_func]):
 ^^^
 do_something()

 Presumably you mean to actually call those functions, as checking
 the identity of a costly function is still cheap :)

 Which is what I do...calling only those necessary until the all/any
 condition has been met. :-)

 If you create the list of things to iterate over by calling them as
 you create the list, then you don't save much of anything.  If you
 only call until one of them breaks the any/all construct, you save
 all the subsequent function calls.

*facepalm* Yep, you do indeed. My bad! Take no notice of the man
behind the curtain...

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Steven D'Aprano
On Sat, 29 Mar 2014 17:07:20 -0400, Roy Smith wrote:

 I certainly agree that things like
 
 if a is not b is not None: ...
 
 belong in an obfuscated coding contest. 

Apart from the fact that I got it wrong (that's what happens when I post 
at 6am after being up all night, thanks for the correction Lele), if you 
consider chained comparisons to be obfuscated, I think you're not 
really fluent at Python. The OP even suggested  `a != None != b` so I 
think that (s)he at least understands chained comparisons.

However, I agree with Johannes that inverted conditions (using not) are 
sometimes harder to reason about than regular conditions.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Chris Angelico
On Sun, Mar 30, 2014 at 1:04 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 On Sat, 29 Mar 2014 17:07:20 -0400, Roy Smith wrote:

 I certainly agree that things like

 if a is not b is not None: ...

 belong in an obfuscated coding contest.

 Apart from the fact that I got it wrong (that's what happens when I post
 at 6am after being up all night, thanks for the correction Lele), if you
 consider chained comparisons to be obfuscated, I think you're not
 really fluent at Python. The OP even suggested  `a != None != b` so I
 think that (s)he at least understands chained comparisons.

 However, I agree with Johannes that inverted conditions (using not) are
 sometimes harder to reason about than regular conditions.

Chained comparisons where you're checking a single variable against
two constants make perfect sense:

2  x  5

Chained comparisons where you check a single constant against two
variables don't, so much:

x  2  y

What exactly does that mean, and why is it written that way? We can
figure out how the interpreter will parse that, but does that
correspond to the programmer's intention?

It'd be more useful but less clear if one of the conditions points the
other way:

x  2  y

which checks that they're both less than two, but IMO in a less-than-clear way.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Steven D'Aprano
On Sat, 29 Mar 2014 17:36:55 -0500, Tim Chase wrote:

 And for cases where you have more than one or two things to test for
 None-itude, you could use
 
   if all(x is None for x in [a, b, c, d]):
 do_something_if_theyre_all_None()
 
 or
 
   if all(x is not None for x in [a, b, c, d]):
 do_something_if_no_Nones()
 
 or
 
   if not any(x is None for x in [a, b, c, d]):
 do_something_if_no_Nones()
 
 which I find *much* more readable from a maintenance point of view.

With one or two things, I would stick to a regular comparison (skipping 
the not):

a is None
a is b is None

With three, I would consider either idiom:

a is b is c is None
all(x is None for x in (a, b, c))

but lean towards the use of all(). From four onwards I would definitely 
use all(), and of course if there is an arbitrary number of items, I 
would definitely use all().



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Roy Smith
In article mailman.8709.1396145720.18130.python-l...@python.org,
 Chris Angelico ros...@gmail.com wrote:

 Chained comparisons where you're checking a single variable against
 two constants make perfect sense:
 
 2  x  5
 
 Chained comparisons where you check a single constant against two
 variables don't, so much:
 
 x  2  y

To me, chained comparisons make intuitive sense when they're all  (or 
=).  I just think back to junior high school algebra class, with the 
big number line above the blackboard.  Thus,

 a  b  c

means if you put a, b, and c on the number line, a is to the left of b, 
which is to the left of c.  I have no problem extending that to more 
than three values:

 a  b  c  d  e

still makes intuitive sense.  I have no particular problem with

 x  2  y

because it fits the same pattern.  But, if you show me

 a != None != b:

my brain just goes into overload.  Honestly, I don't even know what that 
means.  My brain keeps trying to stick a, None, and b on Mrs. Albaum's 
number line and keeps walking into the wall.  If you (the editorial you) 
tell me that my failure to grok that expression means I'm not fluent in 
Python, well then, guilty as charged.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Roy Smith
In article 5337807b$0$29994$c3e8da3$54964...@news.astraweb.com,
 Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:

 a is b is c is None

And we are all together.  See how they run like pigs from a gun, see how 
they fly.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Rustom Mody
On Sunday, March 30, 2014 8:09:45 AM UTC+5:30, Roy Smith wrote:
 I have no particular problem with

  x  2  y

 because it fits the same pattern.  But, if you show me

  a != None != b:

 my brain just goes into overload.  Honestly, I don't even know what that 
 means.  My brain keeps trying to stick a, None, and b on Mrs. Albaum's 
 number line and keeps walking into the wall.  If you (the editorial you) 
 tell me that my failure to grok that expression means I'm not fluent in 
 Python, well then, guilty as charged.

Math Terminology
A relation that is reflexive antisymmetric and transitive is a partial order
Strict order: Irreflexive asymmetric and transitive
Both are strongly related
For general R (partial) S (strict)

S from R
xSy = xRy ∧ x ≠ y
R from S
xRy = xSy ∨ x=y
/Math Terminology

For both these chained comparisons are natural

!= is not transitive: 2 != 3 and 3  != 2 ⊬ 2 == 2

So for != chained comparisons are not natural (or IMHO appropriate)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: checking if two things do not equal None

2014-03-29 Thread Zachary Ware


On March 29, 2014 9:43:00 PM CDT, Roy Smith r...@panix.com wrote:
In article 5337807b$0$29994$c3e8da3$54964...@news.astraweb.com,
 Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:

 a is b is c is None

And we are all together.  See how they run like pigs from a gun, see
how 
they fly.

I'm cryin'.

(Really, that was terrible.)

Walrus-ly y'rs,

Zach

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


Re: checking if two things do not equal None

2014-03-29 Thread Steven D'Aprano
On Sun, 30 Mar 2014 13:15:18 +1100, Chris Angelico wrote:

 On Sun, Mar 30, 2014 at 1:04 PM, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
 On Sat, 29 Mar 2014 17:07:20 -0400, Roy Smith wrote:

 I certainly agree that things like

 if a is not b is not None: ...

 belong in an obfuscated coding contest.

 Apart from the fact that I got it wrong (that's what happens when I
 post at 6am after being up all night, thanks for the correction Lele),
 if you consider chained comparisons to be obfuscated, I think you're
 not really fluent at Python. The OP even suggested  `a != None != b` so
 I think that (s)he at least understands chained comparisons.

 However, I agree with Johannes that inverted conditions (using not)
 are sometimes harder to reason about than regular conditions.
 
 Chained comparisons where you're checking a single variable against two
 constants make perfect sense:
 
 2  x  5
 
 Chained comparisons where you check a single constant against two
 variables don't, so much:
 
 x  2  y
 
 What exactly does that mean, and why is it written that way? 

It checks that 2 is strictly bounded between x on the left and y on the 
right, i.e. that 2 is inside the open interval x...y. I don't know why 
you think that's unclear. But then I do have a maths background and I'm 
used to chaining comparisons.

Write it like this:

low = x
high = y
a = 2

low  a  high

Does that make more sense? Well-chosen names are good. The fact that a is 
a constant rather than a variable is no big deal:

low  2  high


 We can
 figure out how the interpreter will parse that, but does that correspond
 to the programmer's intention?

That applies to just about anything:

(x % 2 == 1) or (x  0)

What that my intention, or did I intend to write

(x % 2 == 0) and (x  0)


At some point you just have to accept that, in the absence of clearly 
nonsensical code or a contradiction between the documentation and the 
behaviour (i.e. a bug), the programmer will have written what she 
intended to write.


 It'd be more useful but less clear if one of the conditions points the
 other way:
 
 x  2  y
 
 which checks that they're both less than two, 

which is quite different from what you wrote the first time.


 but IMO in a less-than-clear way.

That's an understatement. If I saw code chaining comparisons in that 
fashion, I would assume the second operator  was a typo.

Chaining less-than and greater than operators should, for clarity, always 
be written in a single order. E.g. a = b  c  d, not a = b  d  c.

(The second contains a subtle bug too.)



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list