Software Designer Mindset Flashcards
(24 cards)
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
Functional vs Unit Test
Functional tests the app from the outside user perspective
Unit tests the app from inside programmer perspective
How does TDD approach functional and units test?
Start by writing a functional test describing the new functionality from the users point of view
Once you have a functional test that fails start to think about how to write the code that can get it to pass.
Now use one or more unit tests to define how the code should behave. Every line of production code should be tested by one of our unit tests.
Composition vs Inheritance
Both are methods to establish relationships between classes and objects
Inheritance derives one class from another
Composition defines the class as the sum of its parts
Inheritance is used for…
polymorphism, where you have a base class and want to extend its functionality
Composition is a what type of relationship
has-a
public class Job{}
How would you use composition to create a relationship with this class
public class Person{
private Job job; }
Person has-a job
What type of relationship does inheritance create?
Is-a
public class Animal{}
How would you use inheritance to create a relationship with this class.
public class Cat : Animal {
}
Use inheritance only when you know the super class will…
Not be changed
Otherwise because the subclass is tightly coupled to the superclass there could be error introduced upon changes.