You Can Do Really Cool Things With Functions In Python
Video Title: You Can Do Really Cool Things With Functions In Python
The most common use of functions in a piece of code is to group operations and then call that function one or more times from another place in the code but functions are way more flexible than that in python functions are objects of type callable you can define type
Aliases for them you can pass functions as arguments to other functions if you use a package like phone tools you can even call functions partially it’s better than sliced bread and i don’t even like sliced bread have you noticed that in whatever country you are the people living there think that their
Bread is better than in any other country it’s really weird i mean i live in the netherlands so for us it doesn’t really apply because our bread is actually better but if you live in one of those other countries so basically living in a lie how do you
Cope with that another thing we’re really good at in the netherlands is writing software design guides for example this one aryancodes.com design guide it’s totally free it contains the seven steps that i take whenever i design a new piece of software from scratch i’m sharing it
With you so i hope it helps you to avoid some of the mistakes that i made in the past and while you’re at it order some dutch bread or you know keep living in your bread fantasy world choice is yours ironcodes.com design guide and link is also in the description of this video
Today i’m going to show you a couple of not so common ways of using functions in your code as an example i’m going to use a trading system with different trading strategies so it’s a bit like the stranger pattern but we’re going to do some cool stuff with functions let’s dive in
So there’s a trading strategy protocol class here that has a should buy and a should sell method and both of these in the protocol they’re not implemented so they raise a not implemented error if you scroll down you see that we have an average trading strategy that looks at the last few prices
And then checks whether it’s below or above the average and then decides whether or not to buy based on that number and then we also have a min max trading strategy that looks at a minimum price and a maximum price if the price is below that minimum price then we
Should buy it if it’s above a certain maximum price then we should start selling the asset that’s the whole idea of these two strategies of course these strategies will never make you any money because the market is way more efficient than that so don’t use this for real
Trading it’s just meant as an example then we have a class trading bots that’s a data class and it has an exchange and a trading strategy the exchange is something that i just built myself very simple it has one constant with some price data so it doesn’t actually
Connect to an exchange and we have a method for getting particular market data that gets fake price data for us and then we have something that simulates buying and simulates selling so the trading bot has an exchange and it has a trading strategy which is an instance of well one of these classes
Average or min max and then there’s a run method that gets a symbol the type of currency that you want to buy or sell that method does a few things it gets the price data from the exchange it determines whether we should buy or sell and then it’s going to buy
Or sell a certain quantity of that symbol and otherwise that’s going to print no action needed and then a simple main function that creates an exchange connects to exchange creates a trading strategy and then a trading bot and then runs it for bitcoin my favorite cryptocurrency i actually bought some
Bitcoin and ethereum again a while ago so bitcoin ethereum to the moon let’s run this and see what happens so in this case it’s going to sell 10 satoshi probably in the bitcoin us dollar market but i can also change now the trading strategy to something else for example let’s use the average
Trading strategy and then what’s going to happen is we’re actually going to buy something so these strategies they have different effect on the behavior of the trading mod which makes total sense right now this is a traditional implementation of the strategy pattern we have an abstract class or in this
Case a protocol class that defines the interface that has a methods or in this case two methods so it’s not entirely traditional strategy pattern but it’s pretty close then we have subclasses or classes that implement the protocol that have these two methods should buy and should sell and then there’s another
Class that uses it so that’s the traditional object oriented approach of doing this and because object oriented it relies heavily on classes if you’re not too attached to using classes if you don’t store state functions provide a great way to achieve the same effect as the strategy pattern in this example in
General functions can replace or significantly simplify code that uses classes and object-oriented design patterns that doesn’t mean that design patterns aren’t needed anymore they’re still useful but in many cases a functional approach might actually be much simpler so let’s change this code to use functions instead of classes
Basically if you look at the signature of these methods each of them gets a bunch of prices and then returns a boolean value so in order to transform this example into using a more functional approach instead of all these classes we should perhaps define this function type before we start doing that
Job so what i’m going to do is create a trading strategy function which is going to be our function type and that’s going to be a callable and the input is going to be the list of prices that’s a list of integers and what it’s going to return is a
Boolean value so this is basically what any of the trading strategy functions are going to look like so this class i’m going to delete because we’re not going to use the class anymore and then we have our average trading strategy functions and we have the min max trading strategy functions and we should
Simply transform them into functions that follow this signature so let me also remove this class definition because we don’t need that anymore and i’m going to take these lines here and let’s de-indent them i’ll remove the self and the same thing here so remove the cell so we have should buy and should
Sell and then let’s also change the name so that we know that this is the average strategy so this is should buy average and we have should sell average and we can do the same thing for the min max trading strategy so i’ll just take again this function definitions remove the self
Like so and then we’re going to call this min max instead like so so now we have our functions and now we need to change the trading ball class because no longer has a trading strategy but it has a buy function and a sell strategy function so let’s call that to buy strategy
Which is a trading strategy function and we have the cell strategy which is also a trading strategy function and in the run methods we don’t do this but we call here the buy strategy and here we call the sell strategy like so and now we can create the trading strategy here we can
Delete these lines and we simply provide the functions that the trading bot needs right here so let’s say we could have the should buy average and should sell average like so and now let’s run this code just to verify that this still works so there is a problem here function object has no
Attribute should buy and that’s of course because we don’t need to do the goal here anymore because this is already a function so let’s remove that as well let’s run this one more time and now we’re getting exactly the same but because we’re using the more functional approach now it’s
The code has become a bit shorter because we don’t have all these classes everywhere another advantage is that in the trading bot we can now supply different combinations of functions so we could have a buy strategy for average and we could have a cell strategy which
Is min max and then let me run this one more time and well the result is the same but you can imagine that we can make now any combination that we like so like so and then let’s see what happens then and now there is no action needed
So by using a more functional approach here we’ve also introduced some extra flexibility now of course you could also do this with the regular strategy pattern in a sense that instead of having a trading strategy that has both a should buy and should sell method we could create classes for buying and for
Selling leading to even more classes and subclasses by the way so personally i really prefer this type of approach where we’re dealing with simple functions that we just pass to the training bot in fact even the trading bots we could turn it into a function because at the
Moment it’s a simple it’s a single method we could actually turn this into a simple function that gets a symbol an exchange a buy strategy and a cell strategy and then just runs the strategy and there is no class needed anymore but for now i’ll leave it like it is it’s
Also not a big issue that it’s a class and we might want to add more methods to it in the future one of the issues with the functional version of the strategy pattern that we use that there is no way to pass parameters to these functions and that’s because trading bot expects
These functions to have a particular type it expects these functions to accept a list of prices but what if for the average trading site you want to pass parameters like the window size or for the min max trading strategy we want to set the minimum and maximum price currently we can’t do that
In the original version of the code that used classes you could pass these parameters to the class initializer and then store them as states with functions there are other ways to fix this one of them is by using closures so let’s see how that works what we’re going to do is
Create a function that returns a trading strategy function and then we can pass parameters to that particular function and then it can pass those parameters to the closure that it creates and then it returns a function that we can still use in the trading strategy so for example here we have
Should buy average and we might want to change the window size here currently it’s three we might want to make it bigger or smaller so what we can do instead is use a closure let’s call that should buy average closure and that’s going to get a
Window size which is an integer and what this is going to return is a trading strategy function and then we define the should buy average function inside of this closure function and here the -3 we’re setting that to the window size like so and now what we’re going to do is return
Should buy average so that’s our closure function and because we’re passing the window size as a parameter here we can use it in should by average that’s the whole idea of the closure but the function that we return is still a function that only gets a list of prices
This is the closure mechanism what you can now do in the main function is that instead of passing this function directly we can pass a should by average closure and then we actually pass it the window size so for example i could now pass the window size of four and this is
Going to return us the average function that takes a window size of 4. and now let’s run this and well apparently we still need to buy more bitcoin so this is what a closure does and you can basically use the closure for any of these other functions as well so for
Example for should sell min max it’s probably useful if we have a closure that allows to define what the price is so let’s create that should cell min max closure so this is going to get a max price which is an int and this is also going to return a
Trading strategy function and then this function here is going to be a part of the closure and instead of having this fixed price i’m going to replace it by the max price and then it’s going to return the function as a result like so and this line doesn’t make any
Sense anymore so now we have this should sell min max closure and then i can go here should sell min max closure and i provide a price let’s say i only want to sell if the price is above 35 000 like so and i need to put
Two additional zeros this by the way nice way in python to make bigger numbers more clear in the interface actually does nothing it doesn’t change the value of this number but it makes it a bit clearer how many zeros there are and what it exactly means so that’s why i’m using that here
So should buy and should sell now use a closure so we can set some parameters here and trading bots still doesn’t know anything about that because it simply gets a trading strategy function from it so let’s run this and now we’re still buying 10 cryptocurrencies if i change the window
Here to something else let’s see now i’m not buying anything because the window has changed and apparently we’re just on the limit of whether we should buy or not use enclosure solved the problem of not being able to pass parameters to these functions but it’s a
Bit for both and it’s not very flexible instead a more elegant way of solving this is by using partial functions a partial application or partial function application fixes a number of arguments in the original function and returns a new function without those arguments this is different from querying by the
Way which is not a term you might have heard from the functional programming domain currying means that you transform a function that takes multiple arguments into a sequence of functions that each take only a single argument and because you can query any function this is very useful in theoretical computer science
For example where you want to study functions with multiple arguments and then you can transform them into a simple theoretical model of functions taking only a single argument partial functions are way more practical approach and in this case they can really help to simplify the code and
Make it way more flexible so let’s see how that works so let’s first import partial from functools because that’s what we’re going to use to make these functions a bit more flexible and what i’m going to do now is really simplify these functions right here so instead of making this a closure
What i’ll do is simply add the parameter to the should by average function instead and then i’ll delete this line i’ll delete this line and this will go into d and then so now i have a simple should buy average function and for cell i can also add the window size
In exactly the same way so now should sell also expects a window size for should buy i’m doing exactly the same thing so here i’m going to provide a minimum price like so and should sell is exactly the same so i’m also going to add here a
Maximum price which is an int and then i’ll delete the closure here and i’ll de-indent this and now we have our four functions back and these functions are still very simple they just have a bunch of extra parameters this does mean that now we can’t directly use them in the trading agent
Because this expects a should buy and should sell method that follow exactly the trading strategy function type which expects a list of prices and returns a boolean and now these functions they don’t adhere to this type anymore because well they expect other things as well such as this window size or the
Minimum and maximum price i could get away with it in this particular example by supplying default values here and then you could still use them directly but then it would still be impossible to basically set it to a different value than the default value and we actually want to change these things so
We need a better solution and partial can really help here so what we can do with partial is create partially applied versions of these particular functions so what you can do is and that’s actually really neat we can create a buy strategy which is then a partial application so
I’m using parcel here of the function which is let’s say should buy average and we’re going to supply the arguments so we’re going to supply a window size of four and now what’s interesting is that we get back from partial another function like the closure but with the window
Size of four so now we can pass to the trading bots the by strategy function like so and as you see the type arrows are gone because well by strategy is actually a function that returns a boolean and that takes a list of prices and similarly we can create a cell strategy
Which is also partial application of let’s say should sell average or let’s do the uh the min max actually and then we’re going to supply a max price of let’s say 35 000 dollars like so and now what i can do here is supply the cell strategy and let’s
Run this see what happens so we get some missing parenthesis somewhere oh i think this one doesn’t need to be there let’s run that again and now we’re back at a working state of the code again so this is really powerful because now it means that we
Have flexibility in the way that we define our functions we can supply these extra arguments here max minimum price window sizes etc but because of partial we can transform these functions into new functions with partially applied arguments so this is actually the should by average function except it
Already gets a window size of four we’ve partially applied the function and this is really neat because this is way shorter than using the closure solution that i showed you before and it also allows you to create these functions a bit more independently of the type that something like the trading bot expects
Because you can simply use parcel application to resolve the rest of the arguments and then the trading bot doesn’t have to know anything about that so i think there’s a really neat way of allowing for more flexibility in your code while still keeping things relatively simple so partial function application i love
It i think you should also experiment with it a bit more in your code if you have used partial functions already let me know in the comments what your thoughts are about this so anyway i hope you enjoyed this video and that it gave you some ideas of how you could use
Partial function application to your advantage give the video a like if you enjoyed it consider subscribing to my channel if you want to learn more about software design and development thanks for watching take care and see you soon
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
💡 Get my FREE 7-step guide to help you consistently design great software: https://arjancodes.com/designguide.
I like the last example with "partial functions". Still Composition, but very flexible. Thank You again for your teaching!
I recently discovered a use for the partial function that I find really helps clean up menus in a text prompt interface. Of course, the simple way to write a text prompt menu is to have a series of if clauses to check the user input against to determine what function to call, but that can be a real pain in the bum to make changes to, in part because you have to write a whole new if clause and in part because it's generally helpful to be able to enter “help” to get a list of valid inputs, and when using a series of if clauses, you have to remember to manually maintain that list.
So what I've started doing is defining a dictionary for each menu, where each key is a valid input the user can enter, and a key’s corresponding value is a partial function call, which is useful because not every function necessarily takes the same global objects; in the program I'm currently working on, I'm using SQLite, so most functions just need the cursor object, but the save function needs the database connection object in order to call the commit method on it, so for most functions, I can bind the cursor to the partial call, but for the save function, I can instead bind the connection, so in any case, I just need to call the function with no arguments. And as for entering “help” to get a list of valid inputs, you can get that list by just looking up the keys of the dictionary, so the list is always accurate.
There is a potential gotcha in my framework, as it can be tempting to look up the function in the dictionary and call it on the same line, but of course, looking up the function needs to be in a try/except clause to catch a KeyError in case the user enters an invalid input. So if you look up the function and call it on the same line, as opposed to looking up the function and saving it as a variable and then calling it on a separate line, you will accidentally catch any KeyError that gets thrown anywhere deeper in the program. So it's important to put the lookup line in its own try/except clause.
I've to strongly disagree about your bread claim. My wife is dutch because of this reason, i've been in the NL quite often. Dutch bread sucks. It's too soft and has not much structure – it's best described as "retiree – bread". Your content is brilliant though 🙂
The bread joke really backfired. Of course the Germans have the best bread in the world. After all, we have around 3200 types of bread! Even the Dutch can't keep up with that!
5:15 how exactly the TradingStrategyFunction function type is used after its definition? Good video btw, love your content!
You obviously haven't tried Austrian bread , sliced or not
partials are brilliant
DUTCH BREAD IS TERRIBLE!!!
Like many good things. It's really convenient and right when your team knows each other about it.
What's the advantage of using partial vs. a lambda function that does the same thing?
lol. Better than sliced bread.. I like ur humour ! 😁
I'm american and I'll readily admit that the netherlands does most things better than the rest of the world. One of my absolute favorite places to visit.
Thank you for the great tutorial and all the interesting Python content on your channel!
Funny , makes me think of someone moving back to C from C++.
when the functions all need to use the window_size , I think it is better to pack the functions to a class. ????
What is the name of the theme you use in VS Code? I love the colour scheme of syntax highlighting in your videos.
🤣
Your bread tangent made me laugh a lot, thank you haha
The bot knows. Always buy more Bitcoin, sometimes take a break from buying Bitcoin then, buy more. Never sell 😂
Fk yo bread!
But this vid is gold
Do partials have any affect on time complexity of a function? Say you have a function where the part that is partially applied takes O(n^2), and the remaining part would be O(n). When calling the partially applied function, would this be O(n), because the O(n^2) part has already been handled or O(n^2) because it hasn't? This seems quite niche, but im currently working on graphical demonstration of time complexities and partials could be very helpful as I could only look at timing a specific part of a function. I watched this video a while back and remembered how useful I found it
I'm from the US, and I can vouch for the excellent NL accent! …and I'll grant you the bread assertion
My mother used to make bread with recipes that undoubtedly came from the Netherlands (as my country was a Dutch colony and before that we have no bread) and I'll say they're great! 👍🏽
ahaha arriving to this video and realized to things. first youre Dutch! second … Flamands say their bread is the best (Live in Antwerp).
Lindo!!
It's nice that you can replace interfaces and their implementations with partial functions. However I'm still concerned with what happens in cases where we need to do some error validation and type checking, any ideas?
when explaining this topic you should really cover what happens to the call stack and heap … to give students a true view of what is going on. this a bad video overall… shows me you have someone limited understanding of the basic principles … but few other videos you got are good! 🙂 this 1 sucks!!!
are you not reinventing classes with all the closures?
I'm not sold on closures. Besides encapsulation what are they good for?
You said 35_000_00 is thirty five thousand, but you also said that the underscores doesn't change anything about the integer value so it would be 3500000 so that wouldn't add 2 decimals randomly? So it should be 3_500_000 which is 3,5 million?
17:55 where did the partial grab the prices from however if it was not told..?
I live in the US and the bread in the Netherlands IS better. I question anything I eat in the US if bacteria doesn't even want to eat it.
Seeing how must cultures use it as a comfort food,
I'd say that bragging about your bread, is the cope, I don't really care either way.
Thank you for your time and patient, the DOC contains a beautiful explanation in 7 steps about design better.
I just discovered a disappointing downside of functools partial – partially applied functions don't typecheck!
I use mypy to check my type hints. I have a type hint for a "validate_multiple" function that expects a list of Callables with a certain validator signature. I tried to pass a partial to it and got this error:
Argument 3 to "validate_multiple" has incompatible type "List[object]"; expected "Sequence[Callable[[Dict[Any, Any], ExecutionContext], Dict[Any, Any]]]
Apparently there's an ugly workaround that involves manually casting the partial back to the right type, but that's not satisfying at all. 🙁
I live in the United States. I just keep my mouth shut about bread lol
Um excuse me sir I disagree. I am from Amurrica and we have the #1 absolute WORST bread on the planet, hands down, no questions aksed. Thank you very much.
Also, I really dig your vids. There's a lot in them that is over my head, but I keep tryin.