Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Flattening the Interface - REPL

00:00 So to check out your new interface, please open up the REPL and import surpluscalculator.

00:08 Now I’m going to import it as sc just to save a little bit of time. So to check out the namespace, do dir() of sc, and then when you hit Enter, you see a bunch of dunder methods again, but you also see the functions that the user will be needing. So there’s calculate_fixed_income(), calculate_fixed_outgo(), calculate_random_income(), and calculate_random_outgo(). So now the functions that the user needs are directly available to them.

00:40 So how does that simplify the code? Well, you still need your income_function as before, but now all you need to type is sc dot and then choose whichever one you want, calculate_random_income(), for example.

00:56 The benefit, of course, being that you know what the name of that function is because it now appears in the public interface. There you go. And then your income_inputs.

01:07 Again, choose a list, 500 to 2,000, I think I used before. And then the outgo_function.

01:21 Well, again, sc dot and then choose from your public interface, calculate_random_outgo().

01:34 You then need the outgo_inputs. And that’s again a list of, I forget what I chose, I think 1,000 to 1,500. And then you need to do the simulation.

01:49 And again, simulate_surplus() appears up here in your public interface. So sc.simulate_surplus() with its four input parameters as before.

02:02 So income_function,

02:07 income_inputs, outgo_function,

02:14 and outgo_inputs.

02:18 So it’s important to see that this user interface is far more straightforward to use than the user interface of the basic example because it addresses the two key issues that the basic example’s user interface had.

02:33 Firstly, now in this example, the user doesn’t need to understand or know the structure of the code anymore. For example, the user doesn’t need to know that simulate_surplus(), which is a function the user needs to use, that that function lives in the run_sims module that itself lives in the simulations subdirectory.

02:53 The user doesn’t need to know that anymore because simulate_surplus() is now available in the public interface. And secondly, the user also doesn’t need to remember the names of the functions to use because just as simulate_surplus(), the names of the other functions just appear in the public interface.

03:12 So mission accomplished. In the next lesson, you’ll learn about wildcard imports.

Become a Member to join the conversation.