Week 5 Flashcards
What is __str__ and what does it do?
It is the string magic method.
It returns a human readable string representation of the object.
What is __repr__ and what does it do?
It is a magic method for the representation of how to recreate the object.
It returns an unambiguous representation of the object.
What does the __add__ method do?
It defines the behavior for the + operator between two objects
What is __dir__ and what does it do?
It is the directory magic method
It returns a list of the valid attributes and methods for the object
What is __bool__ and what does it do?
It is the boolean magic method
It returns the defined boolean value for the object
What is __init__ and what does it do?
It is the constructor magic method and it defines the attributed within the object.
What is the __len__ magic method?
It defines the length for the given object
How do you define a subclass?
class Sub_class(Base_class):
When do you raise exceptions?
If you don’t have control over input
When do you use assertions?
Whenever code is checking input or conditions you have control over
How do you gather info on an unknown exception?
except Exception as e:
Optional: print(e)
How do you use the super() method in __init__?
def __init__(self, name):
super().__init__(name)
What is a polymorphic function?
A function that applies to many (ploy) forms (morph) of data
What is a generic function?
A function that can apply to arguments of different types
What is type dispatching?
It allows you to change what the function does based on the type of input