12 Object-oriented Design Flashcards

(28 cards)

1
Q

Attributes

A

Data that belong to an object
Feature of a class
e.g. type, make and model of class Vehicle

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

Methods

A

functions that are associated with a class, and perform operations on the data associated with an instantiation of the class

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

class

A

Blueprint for creating object

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

Object

A

an instance of a class (real thing created from the blueprint)

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

What happens when you define a class

A

You need to define a set of Attributes

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

Object

A

An instance (example) of a class

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

What is the shape attribute of a NumPy array?

A

Describes the number of entries in the array along each dimension

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

How can you tell the difference between an attribute and a method?

A

Attribute accessed without parentheses and methods use them since they are functions

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

Why do attributes not use parentheses?

A

Because accessing an attribute does not perform computation -just retrieves stored data

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

Can object methods take other objects as arguments?

A

Yes

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

Where can you find a list of class attributes and methods

A

In class documentation

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

Why might you create your own class?

A

When no existing class suits your problem

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

What does a class represent?

A

A blueprint for creating objects with attributes and methods

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

what is the purpose of the __init__ method?

A

It initialises an object when it is created and sets up its attributes

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

what does self refer to in a class?

A

The object itself

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

What typically goes inside the __init__ method?

A

Assignment that define the object’s attributes

17
Q

What is the role of class methods?

A

To perform operations using the object’s attributes

18
Q

What are magic methods?

A

Special methods with double underscore that define how operators behave

19
Q

Why are magic methods usually not called directly?

A

Because python calls them automatically when operators are used

20
Q

What happens if you se an operator that a class does not define

A

Python raises an error

21
Q

What does the __str__ method control?

A

how an object is displayed when printed

22
Q

How can you redefine how mathematical operators work for a class?

A

By implementing the corresponding magic methods.

23
Q

What is the benefit of defining custom operator behaviour?

A

It allows objects to work naturally with Python syntax.

24
Q

Why do built-in sorting functions work with many object types?

A

They rely only on comparison operators, not object details.

25
How can you sort objects using a custom rule without rewriting a sorting algorithm?
By defining comparison operators in the class.
26
What does custom sorting demonstrate about object-oriented programming?
Algorithms can work generically while objects define their own behaviour.
27
Do you need to implement all magic methods in a class?
No, only the ones you intend to use.
28
Is it possible to call magic methods directly?
Yes, but it is not recommended—operators should be used instead.