Search⌘ K

Everything Is An Object

Explore Python's core concept that everything is an object. Understand how functions, modules, classes, and their instances can be treated as first-class objects with attributes and methods, enabling flexible and powerful programming techniques.

We'll cover the following...

In case you missed it, I just said that Python functions have attributes, and that those attributes are available at runtime. A function, like everything else in Python, is an object.

Run the interactive Python shell and follow along:

Python 3.5
import humansize #①
print(humansize.approximate_size(4096, True)) #②
#4.0 KiB
print(humansize.approximate_size.__doc__) #③
#Convert a file size to human-readable form.
# Keyword arguments:
# size -- file size in bytes
# a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
# if False, use multiples of 1000
# Returns: string

① The first line imports the humansize program as a module — a chunk of code that you can use interactively, or from a larger Python program. Once you import a module, you can reference any of its public functions, classes, or attributes. Modules can do this to access functionality in other modules, and you can do it in the Python interactive shell too. This is ...