Comparisons¶
Every object has an identity, a type and a value.
identity: an integer id that is garunteed to be unique and constant
throughout its lifetime.
is compares two objects for identity.
type: isinstance(object, classinfo) built-in function is recommended
for testing the type of an object, because it takes subclasses
into account.
issubclass(class, classinfo) returns True is class is a subclass
(dirent, indirect, or virtual) of given class.
value: == compares two objects for equality
Tests¶
Membership¶
if 1 in [1, 2, 3]:
pass
if 4 not in [1, 2, 3]:
pass
if 1 in {1: 'one', 2: 'two', 3: 'three'}:
pass
if 'bob' in 'hello bob':
pass
Truth¶
true_value = "aaa" # truthy
false_value = [] # falsey
none_value = None
if true_value:
pass
if not false_value:
pass
if none_value is None:
pass
Type¶
numbers = [1, 2, 3, 4, 2, 5]
if isinstance(numbers, list):
pass