Part 4 · Section 02
Classes
A class in Python is not a blueprint that exists only at compile time. It’s an ordinary object, created when the class statement runs, with its own namespace that you can inspect and change while the program is running. Instances are objects too, each with a namespace of its own. Once you see how Python searches these two namespaces, and what it does when the thing it finds is a function, most of the “magic” of object-oriented Python turns into a few simple rules.
In this section you’ll learn:
- what a class object is, how it relates to
type, and how calling a class creates an instance - class attributes,
getattr/setattr/delattr, and the class__dict__(a read-onlymappingproxy) - instance namespaces, and how an instance attribute can shadow a class attribute
- why a function becomes a bound method when you reach it through an instance, and what
selfreally is - initializing instances with
__init__, and adding attributes and even methods at run time withtypes.MethodType - properties: the
propertyclass, the decorator syntax, and read-only, computed, cached and deletable properties - class methods and static methods, and when to use each
- the class body scope, and why methods and comprehensions can’t see names defined in it
Lectures
- 01Objects and Classes✓
- 02Class Attributes✓
- 03Callable Class Attributes✓
- 04Classes are Callable✓
- 05Data Attributes✓
- 06Function Attributes✓
- 07Initializing Class Instances✓
- 08Creating Attributes at Run-Time✓
- 09Properties✓
- 10Property Decorators✓
- 11Read-Only and Computed Properties✓
- 12Deleting Properties✓
- 13Class and Static Methods✓
- 14Class Body Scope✓