u09_slides-advanced-classes-flashcards

1
Q

What is the root/base class for all Python classes?

A

The ‘object’ class - all classes inherit its special methods

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

What happens by default when using == to compare custom class instances?

A

It falls back to a reference/identity check (equivalent to ‘is’ operator)

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

What is operator overloading?

A

Changing the default behavior of operators (like ==, +, *) for custom classes by implementing special methods

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

What special method is called when using the == operator?

A

__eq__(self

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

What should __eq__ return if the comparison isn’t supported?

A

Return NotImplemented

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

What special method enables addition (+) for custom classes?

A

__add__(self

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

What special method enables multiplication (*) for custom classes?

A

__mul__(self

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

What special method enables index access ([]) for custom classes?

A

__getitem__(self

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

What special method enables the ‘in’ operator for custom classes?

A

__contains__(self

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

How do you make a custom class iterable (usable in for loops)?

A

Implement __iter__(self) method

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

How do you enable context manager support (with statement)?

A

Implement both __enter__(self) and __exit__(self, exc_type, exc_value, traceback) methods

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

How do you make an object callable like a function?

A

Implement __call__(self

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

What special method is used for hashing objects?

A

__hash__(self)

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

What special method is used for string representation?

A

__str__(self)

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

What special method is used for less than comparisons?

A

__lt__(self

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

Where can you find the specifications for special methods?

A

Python’s data model documentation (docs.python.org/3/reference/datamodel.html)

17
Q

Does the object class have any attributes that are inherited?

A

No - object has no instance attributes that are inherited

18
Q

When implementing __eq__ what’s a good practice to include?

A

Check if the other object is an instance of the same class using isinstance()

19
Q

What happens when you use == between objects?

A

Python automatically translates it to a call to __eq__(self

20
Q

What are special methods primarily used for?

A

Enabling Python-specific behaviors and features for custom classes, like operators and built-in function support