09 Classes Advanced Flashcards
What are some of the special methods every class inherits from the base class “object”?
- __eq__(self, other)
- __hash__(self)
- __str__(self)
- __lt__(self, other)
- ..
In Python, what does it mean to “override” a method?
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.
In Python, what is operator overloading?
It means the behavior of an operator (e.g. ==) is changed for a class.
There are some operators that have no default support. What are some we have learned about and how can we provide support for them?
- : __add__(self, other)
- : __mul__(self, other)
- [] : __getitem__(self, key)
- in: __contains__(self, item)
There are some features that have no default support. What are some we have learned about and how can we provide support for them?
- 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)