Usefulness of the "not in" operator
candide
candide at free.invalid
Sat Oct 8 10:41:51 EDT 2011
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.
More information about the Python-list
mailing list