Everything in Python is an object

What's an object?


In [ ]:
o = object()
dir(o)

In [ ]:
o.__class__.__name__

.... everything that inherits from object (which is everything).


In [ ]:
type(obj())

In [ ]:
type(type(1))

In [ ]:
type(type(type(1)))

In [ ]:
class C:
    pass
type(C)

In [ ]:
def f():
    pass

type(f)

In [ ]:
f.__class__.mro()

In [ ]:
C.mro()

In [ ]:
# note the space after the int literal
# it's necessary to resolve ambivalence with '.' in a float
1 .__class__.mro()

In [ ]:
# no ambivalence here
1.0.__class__.mro()

In [ ]:
import os
type(os)

In [ ]:
os.__class__.mro()

In [ ]:
# that means I can pass function or class objects around
# like every 'normal' variable or value
def f():
    print('I am f')
    def g():
        print('I am g')
    return g
print('Call f and get a function back')
h = f()
print('Call the returned function')
h()

Every Python object has a truth value


In [ ]:
help(bool)

In [ ]:
bool(True)

In [ ]:
bool(False)

In [ ]:
bool(0)

In [ ]:
bool(1)

In [ ]:
bool(object())

In [ ]:
bool('')

In [ ]:
bool([])

In [ ]:
bool([False])

In [ ]:
bool({})

In [ ]:
bool('False')

In [ ]:
bool(True)