OOP Flashcards

(40 cards)

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

alternative constructor

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
class DerivedClass(modname.BaseClass):
    ...

derived class
subclass

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__ — 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

getter, setter, deleter methods create copy of property

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

25
Class Attribute
``` class MyClass(): value = ... ``` obj.value class.value shared across all instances
26
Instance Attribute
``` class MyClass(): def ߺߺinitߺߺ(self, value): self.value = value ```
27
Data Class Field
``` @dataclass class MyClass: attribute1: = field() ``` field() parameters: - default - default_factory - init - repr - hash - compare - metadata - kw_only
28
Descriptor
object that defines at least one method of the descriptor protocol ``` class Descriptor: def ߺߺgetߺߺ(self, obj, objtype=None): return ``` to use, must be stored as a class variable: ``` class MyClass: attribute = Descriptor() ``` **obj** is an instance of MyClass **objtype** is the class MyClass optionally can have a __set_name__() method every instance shares descriptor instance dynamic lookup lazy properties
29
Metaclass
class that controls creation and behavior of other classes ``` class MyClass(base1, base2, metaclass=MyMeta): ... ``` type PEP 3115
30
Property Inheritance
``` 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.` ``` 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 ```
31
Descriptor Protocol
descr.__get__(self, obj, type=None) descr.__set__(self, obj, value) descr.__delete__(self, obj)
32
Descriptor Types
only .__get__() – non-data descriptor has .__set__() or .__delete__() – data descriptor data descriptors: property non-data descriptors: classmethod, staticmethod
33
Lookup Chain
- .__get__() of data descriptor - object's __dict__ - .__get__() of non-data descriptor - object type's __dict__ - object parent type's __dict__ (repeat for all parents) - AttributeError
34
Singleton
class with only one instance None, True, False
35
Class Decorator
class that implements **__call__** method ``` class ClassDecorator: def ߺߺinitߺߺ(self, func): self.func = func def ߺߺcallߺߺ(self, *args, **kwargs): result = self.func(*args, **kwargs) return result ``` Use cases: - function objects - decorators - callbacks - command pattern class-based decorator
36
__call__
makes instance of a class callable
37
Decorating Class
``` def decorator(cls): def wrapper(*args, **kwargs): instance = cls(*args, **kwargs) return instance return wrapper ```
38
__slots__
explicitly declares instance attributes and prevents the creation of **__dict__** and **__wekref__** for instances - faster attribute access - space savings in memory ``` class MyClass: ⸏⸏slots⸏⸏ = (par1, par2) ``` special attribute class variable
39
Instantiation
instance creator **.__new__()** instance initializer **.__init__()**
40
Methods
methods are functions