Software Designer Mindset Flashcards

1
Q

Pure Functions

A

Pure functions have no side effects. They don’t change anything.
PureFunc(x,y) -> int:
Return x+y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to make an impure function more pure?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Also ask when creating a function…

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you make a class callable in python?

A

Use the __call__ dunder method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does it mean in python that everything is an object?

A

That literally anything can be treated as an object. Everything has accessible dunder methods. Everything is callable.
print((2).__class__) returns ‘int’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Higher Order Functions

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are closures?

A

A function that you define within a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is one of the uses of python functools?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the Iterable type in python mean?

A

It defines a type that is iterable and not changeable.

post_processors: Iterable[Callable[[int, cust], None]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Instead of interfaces python has what?

A

Protocols

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does super() do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the resulting MRO for the below classes.
Class A:
Class B(A):
Class C(A, B):

A

(A, object)
(B, A, object)
(C, A, B, object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

super() == ?

A

Next in the line of mro

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Functional vs Unit Test

A

Functional tests the app from the outside user perspective

Unit tests the app from inside programmer perspective

17
Q

How does TDD approach functional and units test?

A

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.

18
Q

Composition vs Inheritance

A

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

19
Q

Inheritance is used for…

A

polymorphism, where you have a base class and want to extend its functionality

20
Q

Composition is a what type of relationship

A

has-a

21
Q

public class Job{}

How would you use composition to create a relationship with this class

A

public class Person{

private Job job; }

Person has-a job

22
Q

What type of relationship does inheritance create?

A

Is-a

23
Q

public class Animal{}

How would you use inheritance to create a relationship with this class.

A

public class Cat : Animal {

}

24
Q

Use inheritance only when you know the super class will…

A

Not be changed

Otherwise because the subclass is tightly coupled to the superclass there could be error introduced upon changes.