Software Designer Mindset Flashcards
Pure Functions
Pure functions have no side effects. They don’t change anything.
PureFunc(x,y) -> int:
Return x+y
How to make an impure function more pure?
Customers = {Alice: phone: 1234}
SideEffectFunc() -> None:
Customers[Alice][phone] = 12345
Pass in the thing to be updated.
This also called dependency injection.
LessSideEffect(customers: dict[str, any])
//update customers
Also ask when creating a function…
Is this a pure function. Does it have side effects, if so is there a way I can make it not have side effects such as providing the dependencies as arguments to the function.
How can you make a class callable in python?
Use the __call__ dunder method
What does it mean in python that everything is an object?
That literally anything can be treated as an object. Everything has accessible dunder methods. Everything is callable.
print((2).__class__) returns ‘int’
Higher Order Functions
This idea relies on functions being objects that you can pass along and do something with.
So a higher order function is a function that gets another function as an argument or it can have a function as a return value.
Given a function send_email(cx list) that takes in a list of customers checks if they are of a certain age and sends to only those cxs…
How and why would you make this into a higher order function?
Create another function is_eligible(customer) and pass this function into send_email(cx list, is_eligible)
This allows you to separate out the eligibility criteria from the email functionality.
What are closures?
A function that you define within a function.
What is one of the uses of python functools?
From functools import partial
newFunc = partial(existingFunc, param=25)
Take an existing function, apply ahead of time some of the arguments and you get a new function.
A class is nothing more then a grouping of functions
But there is nothing stopping you from grouping functions in other types of data structures…
What types could you use to create a grouping of funtions?
Create independent functions.
Create a list, set, or tuple of the function names.
Use the list, set or tuple in another function to operate on values.
What does the Iterable type in python mean?
It defines a type that is iterable and not changeable.
post_processors: Iterable[Callable[[int, cust], None]
Instead of interfaces python has what?
Protocols
What does super() do?
Allows you to call the method of a parent class from a derived class.
More specifically the super() takes you to the next object in the __mro__ list. Method Resolution Order
What is the resulting MRO for the below classes.
Class A:
Class B(A):
Class C(A, B):
(A, object)
(B, A, object)
(C, A, B, object)
super() == ?
Next in the line of mro