Object Oriented Program Flashcards

(19 cards)

1
Q

What is the naming convention for classes

A

CamelCaps

ClassNamesAreLikeThis

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

True or false?

All classes have an initializer

A

True

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

How do you define an initializer

A

def __init__(self):

Note the self as it is referencing an object that doesnt exist yet

When a new instance of a class is created, the __init__ method is automatically called, allowing for the execution of setup code and the definition of instance-specific attributes.

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

How would you access the x attribute in the following code?

A

p.x

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

What does the 8th line print?

A

2

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

Revision

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

What does line 8 print?

A

False

p and q are referencing memory locations.

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

What does line 11 print?

A

True

Although p and q reference memory locations, q equals the location of p

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

Why is does the attribute _value have an underscore?

A

To represet a private variable. Meaning it should not be manipulated outside the object. However, python does not enforce this

And so, there should be a method in the class to set the private variable

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

What is the difference between class and object?

A

Class acts as the ‘blue print’, like a function that has not been called yet–defined by class command.
Object is when an instance of the class is created, like assigning one instance class to a name.

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

What is an anonymous variable and how do you represent it?

A

Variables where the value does not matter, like a placeholder. Represented with an underscore

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

What denotes a magic/dunder method?

A

double underscore like __init__(self).

dunder means double underscore

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

What does the 6th line do?

A

By default, all classes have the magic method __str__ which defines what to print in print(class). And by default, it will print the memory location. The 6th line in this code will override this method to make it print something else instead.

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

What does 6th line do?

A

By default, all classes have the magic method __repr__ which defines what to represent the class as when it is called in REPL. And by default, it will represent the memory location. The 6th line in this code will override this method to make it represent something else instead.

Different code but same concept
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

By implementing any of the magic methods into your class, it will override how the binary operations would function.

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

What type of variable does line 2 represent?

A

A class variable. This means that all instance of the class will share that variable

Can be called by calling the class instead of any specific instance

17
Q

What are representation invariants?

A

Conditions or properties that must be always true about a class.

18
Q

Where do you define and enforce representation invariants?

A

They are defined in docstrings and enforced with the assert command

If you have multiple assert lines, always make a method to check them all