Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101838 > unrolled thread
| Started by | Ulli Horlacher <framstag@rus.uni-stuttgart.de> |
|---|---|
| First post | 2016-01-17 09:51 +0000 |
| Last post | 2016-01-19 14:20 -0800 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.python
"x == None" vs "x is None" Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-01-17 09:51 +0000
Re: "x == None" vs "x is None" Chris Angelico <rosuav@gmail.com> - 2016-01-17 21:15 +1100
Re: "x == None" vs "x is None" Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-01-17 11:05 +0000
Re: "x == None" vs "x is None" Peter Otten <__peter__@web.de> - 2016-01-17 12:26 +0100
Re: "x == None" vs "x is None" Chris Angelico <rosuav@gmail.com> - 2016-01-17 22:29 +1100
Re: "x == None" vs "x is None" <paul.hermeneutic@gmail.com> - 2016-01-17 11:01 -0700
Re: "x == None" vs "x is None" Random832 <random832@fastmail.com> - 2016-01-17 16:33 -0500
Re: "x == None" vs "x is None" Chris Angelico <rosuav@gmail.com> - 2016-01-18 08:38 +1100
Re: "x == None" vs "x is None" Ben Finney <ben+python@benfinney.id.au> - 2016-01-18 13:46 +1100
Re: "x == None" vs "x is None" fernando junior <fernandojr.ifcg@live.com> - 2016-01-19 14:20 -0800
| From | Ulli Horlacher <framstag@rus.uni-stuttgart.de> |
|---|---|
| Date | 2016-01-17 09:51 +0000 |
| Subject | "x == None" vs "x is None" |
| Message-ID | <n7fo7q$dso$1@news2.informatik.uni-stuttgart.de> |
I have seen at several places "x == None" and "x is None" within if-statements. What is the difference? Which term should I prefer and why? -- Ullrich Horlacher Server und Virtualisierung Rechenzentrum IZUS/TIK E-Mail: horlacher@tik.uni-stuttgart.de Universitaet Stuttgart Tel: ++49-711-68565868 Allmandring 30a Fax: ++49-711-682357 70550 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-17 21:15 +1100 |
| Message-ID | <mailman.57.1453025729.15297.python-list@python.org> |
| In reply to | #101838 |
On Sun, Jan 17, 2016 at 8:51 PM, Ulli Horlacher
<framstag@rus.uni-stuttgart.de> wrote:
> I have seen at several places "x == None" and "x is None" within
> if-statements.
> What is the difference?
> Which term should I prefer and why?
tl;dr: Prefer "x is None" as a check.
The two checks have slightly different meaning, and almost always you
want the identity check. "x is None" is asking the identity question
"does the name x refer to the exact same object as the singleton
None", but "x == None" is asking the value question "does the object
referred to by x want to consider itself equal to None". For instance:
class NoneLike(object):
def __eq__(self, other):
if other is None: return True
return isinstance(other, NoneLike)
>>> x = NoneLike()
>>> x == None
True
>>> x is None
False
The object referred to as x is not None itself, but it has decided to
call itself equal to None. This is incredibly unusual, and will
generally not be what you want.
As an additional bonus, the identity check is faster. The equality
check has to ask the object if it's equal or not, but the identity
check just says "same object? Y/N", which is generally going to be a
cheap check (in CPython, the objects' pointers can be compared).
But the main reason is semantic - you generally do not _want_ the
possibility of objects comparing equal to None. And if you actually
do, you'd better have some kind of comment there, or someone will come
along and change your code to the more normal "is None".
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ulli Horlacher <framstag@rus.uni-stuttgart.de> |
|---|---|
| Date | 2016-01-17 11:05 +0000 |
| Message-ID | <n7fshb$f36$1@news2.informatik.uni-stuttgart.de> |
| In reply to | #101839 |
Chris Angelico <rosuav@gmail.com> wrote: > On Sun, Jan 17, 2016 at 8:51 PM, Ulli Horlacher > <framstag@rus.uni-stuttgart.de> wrote: > > I have seen at several places "x == None" and "x is None" within > > if-statements. > > What is the difference? > > Which term should I prefer and why? > > tl;dr: Prefer "x is None" as a check. And for the negation? "if not x is None" or "if x is not None" I have seen the last one several times, but i do not understand it, because: >>> x=0 >>> x is not None True >>> not None True >>> x is True False -- Ullrich Horlacher Server und Virtualisierung Rechenzentrum IZUS/TIK E-Mail: horlacher@tik.uni-stuttgart.de Universitaet Stuttgart Tel: ++49-711-68565868 Allmandring 30a Fax: ++49-711-682357 70550 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-01-17 12:26 +0100 |
| Message-ID | <mailman.58.1453030018.15297.python-list@python.org> |
| In reply to | #101843 |
Ulli Horlacher wrote: > Chris Angelico <rosuav@gmail.com> wrote: >> On Sun, Jan 17, 2016 at 8:51 PM, Ulli Horlacher >> <framstag@rus.uni-stuttgart.de> wrote: >> > I have seen at several places "x == None" and "x is None" within >> > if-statements. >> > What is the difference? >> > Which term should I prefer and why? >> >> tl;dr: Prefer "x is None" as a check. > > And for the negation? > "if not x is None" or "if x is not None" > > I have seen the last one several times, but i do not understand it, > because: > >>>> x=0 >>>> x is not None > True >>>> not None > True >>>> x is True > False - "is" has higher precedence than "not" - "is not" is an operator in its own right. So the evaluation is not (x is None) x (is not) None https://docs.python.org/dev/library/stdtypes.html#comparisons https://docs.python.org/dev/reference/expressions.html#operator-precedence
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-17 22:29 +1100 |
| Message-ID | <mailman.59.1453030200.15297.python-list@python.org> |
| In reply to | #101843 |
On Sun, Jan 17, 2016 at 10:05 PM, Ulli Horlacher
<framstag@rus.uni-stuttgart.de> wrote:
> Chris Angelico <rosuav@gmail.com> wrote:
>> On Sun, Jan 17, 2016 at 8:51 PM, Ulli Horlacher
>> <framstag@rus.uni-stuttgart.de> wrote:
>> > I have seen at several places "x == None" and "x is None" within
>> > if-statements.
>> > What is the difference?
>> > Which term should I prefer and why?
>>
>> tl;dr: Prefer "x is None" as a check.
>
> And for the negation?
> "if not x is None" or "if x is not None"
>
> I have seen the last one several times, but i do not understand it, because:
>
>>>> x=0
>>>> x is not None
> True
>>>> not None
> True
>>>> x is True
> False
There's no actual difference:
>>> dis.dis(lambda x: x is not None)
1 0 LOAD_FAST 0 (x)
3 LOAD_CONST 0 (None)
6 COMPARE_OP 9 (is not)
9 RETURN_VALUE
>>> dis.dis(lambda x: not x is None)
1 0 LOAD_FAST 0 (x)
3 LOAD_CONST 0 (None)
6 COMPARE_OP 9 (is not)
9 RETURN_VALUE
The compiler can tell that "not x is None" is exactly the same thing
as "x is not None". Personally, I'd rather spell it "is not None",
partly because it reads more like English that way, and partly because
one operator is better than two; but you're welcome to spell it "not x
is None" if that has other benefits (eg consistency).
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | <paul.hermeneutic@gmail.com> |
|---|---|
| Date | 2016-01-17 11:01 -0700 |
| Message-ID | <mailman.74.1453053678.15297.python-list@python.org> |
| In reply to | #101843 |
I prefer (x is None) and (x is not None). This matches the SQL concept of NULL. (X = NULL) is not valid since NULL is not a value and cannot be compared with anything.
[toc] | [prev] | [next] | [standalone]
| From | Random832 <random832@fastmail.com> |
|---|---|
| Date | 2016-01-17 16:33 -0500 |
| Message-ID | <mailman.75.1453066407.15297.python-list@python.org> |
| In reply to | #101843 |
<paul.hermeneutic@gmail.com> writes: > I prefer (x is None) and (x is not None). > > This matches the SQL concept of NULL. > > (X = NULL) is not valid since NULL is not a value and cannot be compared > with anything. The suitably generic SQL operator is "is (not) distinct from", in some dialects of SQL [certainly if you're using NULL directly you can simply use is/is not, but the "distinct from" operators can be used to compare two expressions that may either or both be NULL. "X is not distinct from Y" == "X = Y or X is NULL and Y is NULL"
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-18 08:38 +1100 |
| Message-ID | <mailman.76.1453066742.15297.python-list@python.org> |
| In reply to | #101843 |
On Mon, Jan 18, 2016 at 8:33 AM, Random832 <random832@fastmail.com> wrote: > <paul.hermeneutic@gmail.com> writes: > >> I prefer (x is None) and (x is not None). >> >> This matches the SQL concept of NULL. >> >> (X = NULL) is not valid since NULL is not a value and cannot be compared >> with anything. > > The suitably generic SQL operator is "is (not) distinct from", in some > dialects of SQL [certainly if you're using NULL directly you can simply > use is/is not, but the "distinct from" operators can be used to compare > two expressions that may either or both be NULL. > > "X is not distinct from Y" == "X = Y or X is NULL and Y is NULL" It's worth noting that Python's None is not really the same as SQL's NULL... although that's partly because nothing is. NULL isn't quite the same as IEEE NaN, nor C's null pointer, nor Python's None, all of which are quite different from each other. All of them can be used to represent some of the same concepts, but they behave very differently. Trying to explain one in terms of another is likely to trip people up somewhere. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-01-18 13:46 +1100 |
| Message-ID | <mailman.83.1453085214.15297.python-list@python.org> |
| In reply to | #101843 |
<paul.hermeneutic@gmail.com> writes: > I prefer (x is None) and (x is not None). There are good reasons to prefer this. But this is not a good reason: > This matches the SQL concept of NULL. That's not really helpful, because it *doesn't* match. > (X = NULL) is not valid since NULL is not a value and cannot be > compared with anything. SQL Null fails comparison with any value. Python ``None`` is a value and can be compared like any other value. SQL Null is neither truthy nor falsy; three-value logic is required when Null can occur. Python ``None`` is falsy by definition, and two-value (Boolean) logic continues to obtain. SQL Null is not a value and has no data type. Python ``None`` is an object and, like any other object, has a type defining the operations that it can perform. And so on. While some libraries do conflate SQL Null with Python ``None``, the two concepts really behave quite differently. It is needlessly misleading to say they “match” in any sense. I'm in the camp that says, while SQL is quite useful, its NULL is a wart <URL:https://dba.stackexchange.com/questions/5222/why-shouldnt-we-allow-nulls/6049>. -- \ “… it's best to confuse only one issue at a time.” —Brian W. | `\ Kernighan and Dennis M. Ritchie, _The C programming language_, | _o__) 1988 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | fernando junior <fernandojr.ifcg@live.com> |
|---|---|
| Date | 2016-01-19 14:20 -0800 |
| Message-ID | <a72124d6-8fdf-4589-b429-0ba5027ca853@googlegroups.com> |
| In reply to | #101838 |
> I have seen at several places "x == None" and "x is None" within > if-statements. > What is the difference? > Which term should I prefer and why? > > > -- > Ullrich Horlacher Server und Virtualisierung > Rechenzentrum IZUS/TIK E-Mail: horlacher@tik.uni-stuttgart.de > Universitaet Stuttgart Tel: ++49-711-68565868 > Allmandring 30a Fax: ++49-711-682357 > 70550 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/ In this case, the Style Guide for Python Code [1] recommends use "is or is not, never the equality operators" [2]. If you use the pep8 tool [3] to check your code, the error code that will be raised is E711 [4]. [1] https://www.python.org/dev/peps/pep-0008/ [2] https://www.python.org/dev/peps/pep-0008/#programming-recommendations [3] http://pep8.readthedocs.org/en/latest/intro.html [4] http://pep8.readthedocs.org/en/latest/intro.html#error-codes
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web