#3 – Object-Oriented Programming (OOP) Flashcards

(10 cards)

1
Q

How do you define a basic class in Python?

A

Use the class keyword.

class Dog:     
def \_\_init\_\_(self, name):         
self.name = name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create an instance of a class?

A

Call the class like a function.

dog = Dog('Fido')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is \_\_init\_\_ used for in a class?

A

It is a constructor method that runs when an object is created.

class Cat:     def \_\_init\_\_(self, age):         self.age = age
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does self mean in a class?

A

self refers to the instance of the class, allowing access to its attributes and methods.

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

How do you define a method inside a class?

A

Define a function with self as the first parameter.

class A:     def say_hi(self):         print('Hi')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a dunder method in Python?

A

A ‘dunder’ method has double underscores (__) before and after. They’re special methods used by Python internally.

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

What is \_\_str\_\_ used for?

A

To define what should be returned when str(object) or print(object) is called.

def \_\_str\_\_(self): return f'My name is {self.name}'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is \_\_repr\_\_ used for?

A

To return an official string representation for developers.

def \_\_repr\_\_(self): return f'Dog(name={self.name})'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is inheritance in Python?

A

A class can inherit from another to reuse its code.

class Puppy(Dog):     def bark(self):         print('woof')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you call a method from the parent class?

A

Use super().

class Puppy(Dog):     def \_\_init\_\_(self, name):         super().\_\_init\_\_(name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly