Except in some extremely rare, hack-y scenarios, you should only see is in the context of is None.
You know, I wanted to mention something similar to this but didn't because we wouldn't usually do if a is None... a better way would be if not a - so I didn't know exactly how to frame it.
There are some corner cases when you would like to use the full comparison form. For example if None is used as an "no argument value was passed" flag:
deffoo(a=None):ifnota:print("No value provided")
it will print message for calls foo(), foo(0), foo(""), foo([]).
Well, Python doesn't have pointers, per se, nor would there ordinarily be a cause to implement your own linked list. It's virtually always best to use Python's own data structures, or at least build upon them.
Well, no, you wouldn't want to do that. if not a is the accepted shorthand for if a == False, but False and None are distinct values. You should always explicitly test for None, although you can implicitly test for "not None":
iffoo:# foo has value (not None) OR foo is True
ifnotfoo:# foo is False
iffooisNone:# foo is None
There's actually a bit more to it than that. if not checks "truthyness", not just equality to False. So not foo will evaluate to True if foo is any of these and more:
False
None
0
[]
{}
""
The same thing applies in the other direction, if [1] will succeed because the list is not empty, empty list are "falsey" whereas lists containing elements are "truthy".
The lesson here is pretty much don't use if without an explicit comparison (== or is) unless you're sure you know how truthy the value will be in all cases.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Very nice, but a word of warning, identity (what
is
checks) is generally a language implementation detail, not something that should be relied upon.Consider:
That same example might not work precisely that way in another implementation of Python.
Mutable and immutable values behave very differently in terms of assignment and identity.
Except in some extremely rare, hack-y scenarios, you should only see
is
in the context ofis None
.Interesting. Thanks for the heads up.
You know, I wanted to mention something similar to this but didn't because we wouldn't usually do
if a is None
... a better way would beif not a
- so I didn't know exactly how to frame it.There are some corner cases when you would like to use the full comparison form. For example if None is used as an "no argument value was passed" flag:
it will print message for calls foo(), foo(0), foo(""), foo([]).
Yes, it's always best to test for the presence of
None
explicitly, rather than implicitly.wouldn't 'is' be used in s scenario where we're comparing pointers like in a linked list?
while p is not q :
p = p.next
I think that is how 'is' should be used.
I've not used linked lists much so I wouldn't know but yeah, I guess that would work.
Well, Python doesn't have pointers, per se, nor would there ordinarily be a cause to implement your own linked list. It's virtually always best to use Python's own data structures, or at least build upon them.
yes.. but if you needed something fancy like a left leaning red black tree or a concurrent skip list you'd need 'is' to work for you for completeness.
(ps. I know they don't really have "pointers" but in these scenarios you use it like a pointer)
Well, no, you wouldn't want to do that.
if not a
is the accepted shorthand forif a == False
, butFalse
andNone
are distinct values. You should always explicitly test forNone
, although you can implicitly test for "not None":Awesome.
There's actually a bit more to it than that.
if not
checks "truthyness", not just equality to False. Sonot foo
will evaluate toTrue
iffoo
is any of these and more:False
None
0
[]
{}
""
The same thing applies in the other direction,
if [1]
will succeed because the list is not empty, empty list are "falsey" whereas lists containing elements are "truthy".The lesson here is pretty much don't use
if
without an explicit comparison (==
oris
) unless you're sure you know how truthy the value will be in all cases.