OOP in Python Flashcards

1
Q

What are the two main things every class has?

A

Attributes and Methods

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

What is the syntax of creating a class in Python

A

class <name>:</name>

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

What is a constructor? How is it written?

A

A constructor is a method that is called when an object is created

def __init__(self):
you can add more parameters

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

How do I make an attribute of the class?
How do I make it private?

A
self.variable
self.\_\_variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when you try to access private attributes?

A

AttributeError

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

What is a crucial parameter that all methods of a class must have (unless it is static)?

A

def <func> (self)
the 'self' parameter</func>

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

What is a static method?

A

It is a method that can be called with the need of an object, mainly used for class utilities:
@staticmethod
def <func_name>():</func_name>

called using class name

<class>.<func>()
</func></class>

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

What if I want the parameters to be unchangeable (read-only)?

A

@property
def <var>(self):
return self.\_\_<var></var></var>

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

How to override the string representation method?

A

def __str__(self):
output = ‘ ‘
return output

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