09 Classes Advanced Flashcards

1
Q

What are some of the special methods every class inherits from the base class “object”?

A
  • __eq__(self, other)
  • __hash__(self)
  • __str__(self)
  • __lt__(self, other)
  • ..
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In Python, what does it mean to “override” a method?

A

It means I change the behavior of the method in a subclass. When calling the method from an object of this subclass, the behavior is different than when calling the method from an object of the superclass.

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

In Python, what is operator overloading?

A

It means the behavior of an operator (e.g. ==) is changed for a class.

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

There are some operators that have no default support. What are some we have learned about and how can we provide support for them?

A
    • : __add__(self, other)
    • : __mul__(self, other)
  • [] : __getitem__(self, key)
  • in: __contains__(self, item)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

There are some features that have no default support. What are some we have learned about and how can we provide support for them?

A
  • Iteration: __iter__(self)
  • Making objects callable: __call__(self, .. )
  • Context managers (in combination with with-statement): __enter__(self) and __exit__(self, exc_type, exc_value, traceback)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly