What does 'is' operator do in Python? -
i (maybe wrongfully) thinking is operator doing id() comparison.
>>> x = 10 >>> y = 10 >>> id(x) 1815480092 >>> id(y) 1815480092 >>> x y true however, val not none, seems it's not simple.
>>> id(not none) 2001680 >>> id(none) 2053536 >>> val = 10 >>> id(val) 1815480092 >>> val not none true then, 'is' operator do? object id comparison conjectured? if so, val not none interpreted in python not (val none)?
you missed is not operator too.
without is, regular not operator returns boolean:
>>> not none true not none inverse boolean 'value' of none. in boolean context none false:
>>> bool(none) false so not none boolean true.
both none , true objects too, , both have memory address (the value id() returns cpython implementation of python):
>>> id(true) 4440103488 >>> id(not none) 4440103488 >>> id(none) 4440184448 is tests if 2 references pointing same object; if same object, it'll have same id() well. is returns boolean value, true or false.
is not inverse of is operator. equivalent of not (op1 op2), in 1 operator. should not read op1 (not op2) here:
>>> 1 not none # 1 different object none? true >>> 1 (not none) # 1 same object true? false
Comments
Post a Comment