OOP Flashcards
Initializer
__init__
__init__
instance method that initializes a newly created object
__dict__
dictionary or other mapping object used to store object’s attributes
class.__dict__ for class attributes
obj.__dict__ for instance attributes
Attribute Access Flow
- instance attributes
- class attributes
Method Object
class MyClass: def func(self): ...
obj.func - method object
class.func - function object
Class Method
@classmethod def method(cls): ...
can be called from an instance
Static Method
@staticmethod def method(): ...
can be called from an instance
__class__
reference to the class of an object
self.__class__
obj.__class__
Parent Class
base class
superclass
Child Class
derived class
subclass
class DerivedClass(modname.BaseClass): ...
Super
super(type, object_or_type=None)
object_or_type determines MRO
zero argument form only inside class definition
Method Resolution Order
C3 linearization algorithm
class.__mro__ — returns a tuple
Mixin
a class that provides functionality to subclasses but is not intended to be instantiated itself
Abstract Base Class
cannot be instantiated
blueprint for other classes
contains one or more abstract methods
from abc import ABC class AbstractClass(ABC): ...
can have concrete methods
Abstract Method
@abstractmethod
from abc import abstractmethod
must be overriden