OOP Flashcards
(40 cards)
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
alternative constructor
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
class DerivedClass(modname.BaseClass): ...
derived class
subclass
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__ — 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
Inheritance Types
Single Inheritance
class inherits from a single base class
Multiple Inheritance
class inherits from multiple base classes
Multilevel Inheritance
derived class inherits from derived class
Hierarchical Inheritance
multiple classes inherit from a single base class
Hybrid Inheritance
multiple types of inheritance
Private and Protected
protected
_protected_attribute
_protected_method
not imported by from module import *
private
__private_attribute
__private_method
AttributeError when trying to access from outside the class
Encapsulation
Name Mangling
_ClassName__private_attribute
_ClassName__private_method
identifier is textually replaced
to avoid accidental overloading of methods and name conflicts with attributes of superclasses
Abstract Class
class that contains one or more abstract methods and is intended to be subclassed
can be defined without using the abc module, typically by raising NotImplementedError in abstract methods
Property
managed attribute
getter — to access the value of the attribute
setter — to set the value of the attribute
deleter — to delete the attribute
getter, setter, deleter methods create copy of property
class attribute that manages instance attributes
descriptor
Property Function
property(fget=None, fset=None, fdel=None, doc=None)
return a property attribute
doc creates a docstring for the attribute
Property Decorator
class MyClass: def ߺߺinitߺߺ(self, value): self._value = value @property def value(self): return self._value @value.setter def value(self, value) self._value = value @value.deleter def value(self): del self._value
only getter method name matters
Del
del class.class_attribute
del class.method
del obj.instance_attribute
prevent removal:
- __delattr__()
- property deleter
Data Class
from dataclasses import dataclass @dataclass class MyClass: attribute1: <Type1> attribute2: <Type2> = <default> ... def method(self): ...
implements:
.__init__()
.__repr__()
.__eq__()
@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)
mutable namedtuple with defaults
Python 3.7
Python 3.6: pip install dataclasses