Usefulness of the "not in" operator

C

candide

Python provides

-- the not operator, meaning logical negation
-- the in operator, meaning membership

On the other hand, Python provides the not in operator meaning
non-membership. However, it seems we can reformulate any "not in"
expression using only "not" and "in" operation. For instance


So what is the usefulness of the "not in" operator ? Recall what Zen of
Python tells

There should be one-- and preferably only one --obvious way to do it.
 
J

Jon Clements

Python provides

     -- the not operator, meaning logical negation
     -- the in operator, meaning membership

On the other hand, Python provides the not in operator meaning
non-membership. However, it seems we can reformulate any "not in"
expression using only "not" and "in" operation. For instance

 >>> 'th' not in "python"
False

 >>> not ('th' in "python")
False
 >>>

So what is the usefulness of the "not in" operator ? Recall what Zen of
Python tells

There should be one-- and preferably only one --obvious way to do it.

You would seriously prefer the later?

Guess I'll have to start writing stuff like:

10 - 5 as 10 + -5 (as obviously the - is redundant as an operation),
and 10 / 2 as int(10 * .5) or something, who needs a divide!?

Jokely yours,

Jon.
 
S

Stefaan Himpe

So what is the usefulness of the "not in" operator ? Recall what Zen of
Python tells

There should be one-- and preferably only one --obvious way to do it.

the zen of python also says (amongst other things):

....
Readability counts.
....
Although practicality beats purity
....

Best regards,
Stefaan.
 
S

Steven D'Aprano

candide said:
So what is the usefulness of the "not in" operator ? Recall what Zen of
Python tells

There should be one-- and preferably only one --obvious way to do it.

And "not in" is the obvious way to do it.


"If the key is not in the ignition, you won't be able to start the car."

"If not the key is in the ignition, you won't be able to start the car."


Who like that second one speaks?
 
A

Alain Ketterlin

candide said:
Python provides

-- the not operator, meaning logical negation
-- the in operator, meaning membership

On the other hand, Python provides the not in operator meaning
non-membership. However, it seems we can reformulate any "not in"
expression using only "not" and "in" operation.

Sure, but note that you can also reformulate != using not and ==, <
using not and >=, etc. Operators like "not in" and "is not" should
really be considered single tokens, even though they seem to use "not".
And I think they are really convenient.

-- Alain.
 
M

Mel

Steven said:
And "not in" is the obvious way to do it.


"If the key is not in the ignition, you won't be able to start the car."

"If not the key is in the ignition, you won't be able to start the car."


Who like that second one speaks?

:)
"If the key is not in the ignition, you will be able to start the car, not."

Mel.
 
J

Jussi Piitulainen

Mel said:
:)
"If the key is not in the ignition, you will be able to start the car, not."

Oh, be consistent.

"If not the key is in the ignition, not you will be able to start the car."

But both negations can be avoided by modus tollens.

"If you are able to start the car, the key is in the ignition."

And one could express "x not in s" as "(x in s) implies False" without
making the "not" explicit if "implies" was in the language. (I know
about <= but I also witnessed an unpleasant thread in another
newsgroup where people insisted that <= should not be defined for
truth values at all, and I also happen to like Python's "not in".)
 
R

Roy Smith

Alain Ketterlin said:
Sure, but note that you can also reformulate != using not and ==, <
using not and >=, etc. Operators like "not in" and "is not" should
really be considered single tokens, even though they seem to use "not".
And I think they are really convenient.

If you want to take it one step further, all the boolean operators can
be derived from nand (the dualists would insist on using nor).
 
C

candide

Le 08/10/2011 14:41, Alain Ketterlin a écrit :
Operators like "not in" and "is not" should
really be considered single tokens, even though they seem to use "not".
And I think they are really convenient.

I realize that I was confused by the lexical form of the "not in"
operator : it is made by juxtaposing two other operators. Operators
composed from two other operators exist, for instance != but the two
cannot be separated, for instance

2 ! = 3

is not a valid expression. Said in another way, in Python syntax,
usually a "lexically juxtaposed operator" is seen as a whole. This is
not the case for an operator such as "is not" or "not in" because for
example

is a valid statement.

A notin operator or isnot operator would be less confusing (at least in
my case ;) ).
 
C

candide

Le 08/10/2011 12:42, candide a écrit :




After browsing source code, I realize that parenthesis are not necessary
("not" has higher precedence than "in").
 
C

candide

Le 08/10/2011 12:50, Jon Clements a écrit :
10 - 5 as 10 + -5 (as obviously the - is redundant as an operation),
and 10 / 2 as int(10 * .5) or something, who needs a divide!?



OK, I see your point but I was supposing non-membershipness seldom
needed and in fact one can suppose that test membership is heavily more
used than test non-membership.

In fact, it seems that most Python operators have an "antonym" operator,
for instance :

== vs !=
< vs >=
is vs is not
+ vs -

etc
 
C

candide

Le 08/10/2011 14:01, Steven D'Aprano a écrit :




And "not in" is the obvious way to do it.

Obvious ? Not so. I performed some code mining and it appears that even
good sources make use of "not (foo in bar)" expressions.


******** begin examples ***************

from drpython/drPluginDialog.py
-----------------------------
if not (plugin in self.parent.pluginstoremove):


from numpy/f2py/crackfortran.py
-------------------------------
if not (l[0] in spacedigits):


from crunchy1.0alpha1/crunchy/src/plugins/vlam_editor.py
----------------------------------------------------------
if (("no_copy" in vlam) and not ("no_pre" in vlam)) or (not python_code):


from Cpython/Python-3.1a1/Lib/logging/__init__.py
--------------------------------------------------
if not (hdlr in self.handlers):

from Cpython/Python-2.6.2/Lib/idlelib/configHandler.py
-------------------------------------------------------
if not (configType in ('main','extensions','highlight','keys')):
raise InvalidConfigType, 'Invalid configType specified'

from
pygtk-2.22.0/gtk/webkitgtk/WebKit-r93015/Source/JavaScriptCore/KeywordLookupGenerator.py
---------------------------------------------------------------------------------------------
if not (key[0] in self.keys):

from pypy/pypy-pypy-release-1.6/lib-python/2.7/logging/__init__.py
------------------------------------------------------------------
if not (hdlr in self.handlers):

******** end examples ***************

_Many_ more examples of this type are avalaible.

The obviousness of an "is not" operator is very debatable. Do you have
standard functions or method such as
isnotinstance, isnotsubset, isnotdir, isnotfile, isnotalpha, etc ?

In my case, It took a long time to realize the existence of a "true"
"not in" operator as I explain in my response to Alain.


Imagine, /Dive Into Python/ book doesn't describe this operator per se
and provides only one source file using it. Official Python tutorial at
python.org didn't provide even one.


"If the key is not in the ignition, you won't be able to start the car."

"If not the key is in the ignition, you won't be able to start the car."


Who like that second one speaks?


Depends if you are aware of negative form conjugation.
 
C

Chris Angelico

A notin operator or isnot operator would be less confusing (at least in my
case ;) ).

Let's replace both of them.

in --> foo extant bar
not in --> foo extinct bar

That would solve the problem, wouldn't it?

*ducking for cover*

ChrisA
 
T

Thorsten Kampe

* candide (Sat, 08 Oct 2011 16:41:11 +0200)
After browsing source code, I realize that parenthesis are not
necessary ("not" has higher precedence than "in").

Lower precedence.

Thorsten
 
D

Dave Angel

Le 08/10/2011 12:42, candide a écrit :





After browsing source code, I realize that parenthesis are not
necessary ("not" has higher precedence than "in").
You should say
"... parenthesis are not necessary ("not" has LOWER precedence than
"in")."
 
C

Chris Angelico

You should say
   "... parenthesis are not necessary ("not" has LOWER precedence than
"in")."

Is "are not" an operator in English, or should this be "not
parentheses are necessary"?

ChrisA
 
G

Grant Edwards

And "not in" is the obvious way to do it.


"If the key is not in the ignition, you won't be able to start the car."

"If not the key is in the ignition, you won't be able to start the car."


Who like that second one speaks?

Yoda.
 
C

candide

Le 08/10/2011 17:16, Dave Angel a écrit :
You should say
"... parenthesis are not necessary ("not" has LOWER precedence than "in")."


I should, yes, I confess ;)


In my defense, I must tell that Python document reference here :

http://docs.python.org/reference/expressions.html#summary


has an anti-iconic way to display the order precedence.


If an operator OP1 has higher precedence than an operator OP2 , it means
that, at evaluation time, you execute OP1 FIRST and operator OP2 later.
So, its seems very natural to present FIRST operator with stronger
precedence.


So, if you iconically present this material in a tabular display, you
present FIRST (ie at the top because usually we process tabular material
starting on the upper part) what you need to calculate FIRST and, again,
you present

at the TOP the HIGHer precedence

and

at the BOTTOM the LOWer precedence.

Documentations usually provide the table in this order (higher to
lower), cf. the table in K&R book for C programing language or here for
the C++ PL :

http://en.cppreference.com/w/cpp/language/operator_precedence

or again there for the Java PL :

http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
 
S

Steven D'Aprano

Roy said:
If you want to take it one step further, all the boolean operators can
be derived from nand (the dualists would insist on using nor).

Let's define the boolean values and operators using just two functions:

def true(x, y):
return x

def false(x, y):
return y


That's all we need to define all of Boolean algebra. Unfortunately, it's a
bit ugly in Python:
<function true at 0xb7c3a36c>

So let's add a helper function to prettify the output:

def pr(b):
print(b(true, false).__name__)
true

Much nicer!


Now define NAND:

def Nand(a, b):
return (lambda c: lambda x, y: c(y, x))(a(b, a))


and we're done. All of boolean algebra can now be derived from Nand.

.... return Nand(b, b)
....
.... return Nand(Nand(a, b), Nand(a, b))
....
.... return Nand(Nand(a, a), Nand(b, b))
....
.... return And(Nand(a, b), Or(a, b))
....false


and so forth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top