OOP Flashcards

1
Q

Initializer

A

__init__

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

__init__

A

instance method that initializes a newly created object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

__dict__

A

dictionary or other mapping object used to store object’s attributes
class.__dict__ for class attributes
obj.__dict__ for instance attributes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Attribute Access Flow

A
  1. instance attributes
  2. class attributes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Method Object

A
class MyClass:
    def func(self):
        ...

obj.func - method object
class.func - function object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Class Method

A
@classmethod
def method(cls):
    ... 

can be called from an instance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Static Method

A
@staticmethod
def method():
    ... 

can be called from an instance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

__class__

A

reference to the class of an object
self.__class__
obj.__class__

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Parent Class

A

base class
superclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Child Class

A

derived class
subclass

class DerivedClass(modname.BaseClass):
    ...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Super

A

super(type, object_or_type=None)
object_or_type determines MRO
zero argument form only inside class definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method Resolution Order

A

C3 linearization algorithm
class.__mro__ — returns a tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Mixin

A

a class that provides functionality to subclasses but is not intended to be instantiated itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Abstract Base Class

A

cannot be instantiated
blueprint for other classes
contains one or more abstract methods

from abc import ABC

class AbstractClass(ABC):
    ...

can have concrete methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Abstract Method

A

@abstractmethod

from abc import abstractmethod

must be overriden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Inheritance Types

A

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

17
Q

Private and Protected

A

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

18
Q

Name Mangling

A

_ClassName__private_attribute
_ClassName__private_method

identifier is textually replaced
to avoid accidental overloading of methods and name conflicts with attributes of superclasses

19
Q

Abstract Class

A

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

20
Q

Property

A

managed attribute

getter — to access the value of the attribute
setter — to set the value of the attribute
deleter — to delete the attribute

class attribute that manages instance attributes

descriptor

21
Q

Property Function

A
property(fget=None, fset=None, fdel=None, doc=None)

return a property attribute
doc creates a docstring for the attribute

22
Q

Property Decorator

A
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

23
Q

Del

A

del class.class_attribute
del class.method

del obj.instance_attribute

prevent removal:
- __delattr__()
- property deleter

24
Q

Data Class

A
from dataclasses import dataclass

@dataclass
class MyClass:
    attribute1: <Type1>
    attribute2: <Type2> = <default>
    ...
 
    def method(self):
        ...

implements:
.__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

25
Q

Class Attribute

A
class MyClass():
    value = ...

obj.value
class.value

shared across all instances

26
Q

Instance Attribute

A
class MyClass():
    def ߺߺinitߺߺ(self, value):
        self.value = value
27
Q

Data Class Field

A
@dataclass
class MyClass:
    attribute1: <Type1> = field()

field() parameters:
- default
- default_factory
- init
- repr
- hash
- compare
- metadata
- kw_only

28
Q

Descriptor

A
class Descriptor:
    def ߺߺgetߺߺ(self, obj, objtype=None):
        return <value>

to use, must be stored as a class variable:

class MyClass:
    attribute = Descriptor()
29
Q

Metaclass

A

class that controls creation and behavior of other classes

class MyClass(base1, base2, metaclass=MyMeta):
    ...

type

PEP 3115

30
Q

Property Inheritance

A
class BaseClass:
    def ߺߺinitߺߺ(self, value):
        self._value = value

    @property
    def value(self):
        return self._value

for a single method:

class DerivedClass(BaseClass):
    @BaseClass.value.setter
    def value(self, value)
        self._value = value

for multiple methods:

class DerivedClass(BaseClass):
    @BaseClass.value.getter
    def value(self)
        return self._value + 1

    # property with modified getter is in DerivedClass
    @value.setter
    def value(self, value)
        self._value = value

or with lambda self: self.<property func>

class BaseClass:
    def ߺߺinitߺߺ(self, value):
        self._value = value

    def _get_value(self):
        return self._value

    def _set_value(self, value):
        self._value = value

    value = property(fget=lambda self: self._get_value(), fset=lambda self, value: self._set_value(value))
class DerivedClass(BaseClass):
    def _get_value(self):
        return super()._get_value() + 1