OOP basics Flashcards

1
Q

What is the main point of OOP in Python?

A

OOP is about code reuse — you factor code to minimize redundancy and program by customizing what already exists instead of changing code in place or starting from scratch.

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

Where does an inheritance search look for an attribute?

A

An inheritance search looks for an attribute first in the instance object, then in the class the instance was created from, then in all higher superclasses, progressing from left to right (by default). The search stops at the first place the attribute is found.

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

What is the difference between a class object and an instance object?

A

Classes are a kind of factory for creating multiple instances. Classes also support operator overloading methods, which instances inherit, and treat any functions nested in the class as methods for processing instances.

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

Why is the first argument in a class’s method function special?

A

It always receives the instance object that is the implied subject of the method call. It’s usually called ‘self’ by convention.

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

What is the __init__ method used for?

A
If the \_\_init\_\_ method is coded or inherited in a class, Python calls it automatically each time an instance of that class is created.
class New():
  def \_\_init\_\_(self, arg1, arg2):
    self.first_var = arg1
    self.second_var = arg2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you create a class instance?

A

You create a class instance by calling the class name as though it were a function; any arguments passed into the class name show up as arguments two and beyond in the __init__ constructor method.

x = ClassName()
y = AnotherClass(arg1, arg2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create a class?

A
class ClassName():
  #some code here
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is inheritance?

A

Inheritance is when a class is based on another class or classes, getting the same methods and attributes from them.

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

What is composition?

A

Composition is when the class-container has in it calls to other classes instead of inheriting from them. So after object is created from container-class this object also composes of objects made by internal calls of included classes.

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

What is a class?

A

Class serves as instance (object) factory. It provide attributes (variables) and methods (functions) that are inherited by all the instances generated from each classes call.

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

What is an instance?

A

Instance is a single item (object) created once by each class call.

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

What are superclasses?

A

They are classes which are used to inherit from.

class Son(Father, Mother): …

In this case Father and Mother are superclasses for Son subclass.

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

What are superclasses?

A

They are classes which are used to inherit from.

class Son(Mother, Father): …

In this case Mother and Father are superclasses for Son subclass.

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

What are subclasses?

A

They are classes which inherit from parent superclasses their methods and attributes.

class Son(Mother, Father): …

In this case Mother and Father are superclasses for Son cubclass.

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

What happens if subclass has it’s own attrs and methods like in it’s superclasses?

A

Own subclasses attrs and methods have higher priority than that from their parents and should replace and override them.

class Mother():
  def \_\_init\_\_(self): pass
  passion_to = 'boys'
  def like(self): print 'I like ' + self.passion_to
class Son(Mother):
  def \_\_init\_\_(self): pass
  passion_to = 'girls'
  def like(self): print 'I LOVE ' + self.passion_to

john = Son()
john.like() # –> ‘I LOVE girls’

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

What is an attribute?

A

It is a single variable declared inside a class. Each instance generated from that class has it’s own copy of this variable which can be also called as object’s attribute.

17
Q

What is a method?

A

It is a single function declared inside a class. Each instance generated from that class has it’s own copy of this function which can be also called as object’s method.

18
Q

What are two ways to call a method?

A

Methods can be called through either

  1. an instance - object.methodName()
  2. or a class - ClassName.methodName(object)
19
Q

How to assign superclasses to the new class during it’s creation?

A
You shall state superclasses inside of parenthesis with a class statement:
class NewClass(ParentClass, AnotherParent)
20
Q

What is a single inheritance?

A
It is when the subclass has only one superclass:
class NewClass(ParentClass)
21
Q

What is a multiple inheritance?

A
It is when the subclass has two or more superclasses, e.g.:
class NewClass(ParentClass, AnotherParent)
22
Q

In which order subclass inherits methods and attrs with the same names from different parents during multiple inheritance?

A

Python’s search order for attrs and methods inside of parent’s parenthesis is left-to-right. So for multiple inheritance the highest priority has first left superclass, if there are the same properties inside of next parents - they’ll be ignored!

class Mother():
  def \_\_init\_\_(self): pass
  def like(self): print 'I like boys' 
class Father():
  def \_\_init\_\_(self): pass
  def like(self): print 'I like girls '
class Son(Mother, Father):
  pass
john = Son()
john.like() # --> 'I like boys'
#john loves boys, wow! so be careful with multiple inheritance order!!!
23
Q

What ‘self’ means?

A

Self is a pointer to the instance being processed.

class SquareContainer():
  def \_\_init\_\_(self, number):
    self.base = number
  def square(self):
    print self.base ** 2

Self is used in two cases:

  1. it is a first argument for every objects method (if you want it to perform actions inside of this obj, because self points to it)
  2. shows for interpreter where to take a variable from; print self.base ** 2; means: ‘take base variable from the current instance of class’
24
Q

Is ‘self’ python’s reserved keyword?

A

No. You can use instead of it any other name as pointer to instance, like ‘this’ from Java and C++. But it is a convention of python developers to use ‘self’ word as a recognizable thing for better readability.

25
Q

Is __init__ a constructor?

A

No way! It’s not a constructor, it doesn’t create an object. (But some autors trying to call it as a constructor. It is totally wrong, as __new__ is constructor actually) __init__ is responsible only for initialization the instance after it’s been created.

26
Q

What is __new__ ?

A

It is the first step of instance creation. It’s called first, and is responsible for returning a new instance of your class.

27
Q

What is the difference between __init__ and __new__ ?

A

__new__ is used when you need to control the creation of a new instance. __init__ is used when you need to control initialization of a new instance.

28
Q

What is polymorphism?

A

Polymorphism means that the meaning of an operation depends on the object being on. For example, ‘+’ operator means different things for numbers and strings. 1 + 1 returns number 2, ‘te’ + ‘xt’ performs concatenation and returns string ‘text’.

29
Q

What is encapsulation?

A

Mechanism called to hide data and methods of classes and objects from user’s override.

30
Q

What is a framework?

A

Frameworks are collections of superclasses that implement common programming tasks as classes, ready to be mixed into your applications.

31
Q

What is base class?

A

Base class is the another name of a superclass, parent you’re inheriting from.

32
Q

What is derived class?

A

Derived class is the another name of a subclass, child which is inheriting from base superclasses.