Classes Flashcards

1
Q

Constructor

A

The constructor is a method that is called when an object is created
_init__

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

Instance variable

A

Is defined at the instance level. It can be changed across the instances

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

Class variable

A

Defined at the class level and is not expected to change

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

How to call class variable

A

Object.class

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

How to change class variable

A

Class.class = change

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

Types of instance methods

A

Accessor and mutator methods

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

How do you define class variable in the function

A

With a decorator @classmethod
And pass cls as an attribute in the function

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

How to define static method

A

With a decorator @staticmethod

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

What is inheritance

A

Is a mechanism that allows a class to inherit properties and behaviors from another class

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

Multiple inheritance

A

Child class passes multiple parent classes as an attribute.

class C(A,B):

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

How does constructor is being called with inheritance

A
  1. If there is no constructor in child class, parent constrictor method is being called
  2. If there is a contractor in child class, child class constructor is being called
  3. To call parent constructor with child constructor:
    super().__init__()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Types of polymorphism

A
  • Duck typing
  • operator overloading
  • method overloading
  • method overriding
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is polymorphism

A

As the word suggests, ‘poly’ means ‘many’ and ‘morph’ points at ‘forms’; thus, polymorphism as a whole would mean ‘a property of having many forms.
One function is doing many different things

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

Duck typing

A

A way of programming in which an object passed into a function or method supports all method signatures and attributes expected of that object at run time.

You do not check types at all. Instead, you check for the presence of a given method or attribute

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

Operator overloading

A

Ability to define or redefine the behavior of build it operators(+,-,*,/<,>, etc) for custom objects or classes

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

Method overloading

A

Two or more methods have the same name but different numbers of parameters or different types of parameters

17
Q

Method overloading implementation

A

When defining variables, account for express possible variable.
Example:
def sum(self,a=None,b=None,c=None):
s=0
if a!=None and b!=None and c!=None:
s=a+b+c
elif a!=None and b!=None:
s=a+b
else:
s=a

18
Q

Method overriding

A

When there are 2 the same methods on child and parent class, child class method takes a priority

19
Q

OOP concepts

A

Class
Objects
Polymorphism
Encapsulation
Inheritance
Data Abstraction

20
Q

What is a class in OOP

A

A class contains the blueprints or the prototype from which objects are being created.

21
Q

What is object

A

The object is an entity that has a state and behavior associated with it.

22
Q

Encapsulation

A

It describes the idea of wrapping data and the methods that work on data within one unit.

To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.

23
Q

How to define private variable for encapsulated concept

A

self.__name = name

24
Q

How to get or change private variables from encapsulation

A

By defining a methods inside the class that returns or changes the variable

25
Q

Data abstraction OOP concept

A

It hides unnecessary code details or sensitive information from the user.

26
Q

How to create an abstract class for data abstraction OOP concept

A

from abc import ABC, abstractmethod

…..

Before method definition add decorator:
@abstractmethod