Recursion and Classes Flashcards
What is a recursive function
A function that calls itself
How to rewrite while to recursive
- Make each local variable a parameter for the function (set default value)
- Replace while with if
- Return value from recursive call with updated parameters
- In else, return answer based on parameter values
How do you make a variable with an empty list a parameter (x = [])?
x: list|None =None
if x == None:
x = []
How can you identify a magic method?
format: __name__
what is the first parameter for any function in a class
self
Documentation for a class
Write a docstring
Annotation for a class
List each attribute and annotate type
For each function annotate all parameters except self
What is the purpose of a class
To store complex data
__repr__ purpose
Choose what calling print on function will show
__init__ purpose
Allows you to call attributes of a class
__init__ annotation
def __init__(self, x: type, y: type) -> None:
__repr__ annotation
def __repr__(self) -> str:
__eq__ purpose
Determine if 2 classes have the same attributes
__eq__ annotation
def __eq__(self, other: any) -> bool:
What is a non-magic method
Methods that are not pre-defined
What is a magic method
Method that is built in
What type of method requires documentation
Non-magic methods