Dependency inversion: write BETTER PYTHON CODE Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Subscribed!! Thank you!

👍︎︎ 2 👤︎︎ u/vidazinho 📅︎︎ Jan 29 2021 🗫︎ replies

Subscribed. Clear and concise example. Thanks!

👍︎︎ 2 👤︎︎ u/80turk 📅︎︎ Jan 29 2021 🗫︎ replies

Subscribed, good job!

👍︎︎ 2 👤︎︎ u/fyzeth1 📅︎︎ Jan 29 2021 🗫︎ replies

Thanks for the tutorial. A list of python modules designed to support this are here:

https://github.com/metaperl/python-oop/wiki/Dependency-Injection--&--Inversion-of-Control

👍︎︎ 2 👤︎︎ u/metaperl 📅︎︎ Jan 29 2021 🗫︎ replies

Hi, you can use ctrl+F2 (or other combination of keys - it is also under context menu "change all occurrences") to refactor the names of variables, instead of changing them one at a time. I am referring to 6:13 in your video.

All in all great example how to do this, keep it up!

👍︎︎ 2 👤︎︎ u/ShanSanear 📅︎︎ Jan 30 2021 🗫︎ replies

Love this! Anything that helps me be more efficient is a gem. So thank you!

👍︎︎ 2 👤︎︎ u/[deleted] 📅︎︎ Jan 30 2021 🗫︎ replies

A short description?

👍︎︎ 1 👤︎︎ u/purplebrown_updown 📅︎︎ Jan 29 2021 🗫︎ replies
Captions
dependency inversion is a key principle that helps you write code you can reuse more easily it's part of a group of design principles called the solid principles the d in solid stands for dependency inversion today i'm going to show you how that works using an example written in python it's a great technique you can apply right away to improve your code i use it all the time a key ingredient of dependency inversion and many of the other design patterns i talk about on this channel is abstraction you need a mechanism in your programming language that allows you to separate the description or definition of the interface from the actual implementation of something for example if you're writing a sorting algorithm your interface might be that your algorithm expects a list of some kind and a function that tells you which element should come first to define such an interface you're also going to need types the input and output of the sorting algorithm has to be a list type for example otherwise there's nothing to sort ideally programming language should support both an abstraction mechanism and types so you can apply these design principles to write better code with python the bad news is that there are no abstraction mechanisms built into the syntax and python also doesn't have types in the classical sense so does this mean that you can't use design principles such as dependency inversion in python the good news is you still can there's a module called abc which stands for abstract base class that you can use to model abstraction and python also has type hints that at least allow you to specify the types of parameters return type of a function and so on they're purely meant for the developer though to increase the readability of the code the interpreter doesn't do any actual type checking there's a module called my pi that does static type checking of python code i haven't tried that myself but i'll put a link to it in the description to see how to apply dependency inversion in python let's take a look at an example to explain the concept of dependency inversion i have an example here with two classes a light bulb and a power switch the light bulb class has a turn on method and a turn off method now obviously i'm not implementing a real light bulb here it's simply a message that's printed that it's either turned on or turned off the electric power switch takes a light bulb as a parameter in its initializing method and then it has a press method that uses to switch on or switch off the light bulb here i create a light bulb i create an electric power switch that gets the light bulb object and then i press the button a few times and this is what happens in this example there's a clear dependency between the light bulb and the power switch because obviously the power switch requires light bulb and then it directly calls the turn off and turn on method on that instance what i'm going to show you is how to use the dependency inversion principle to remove this dependency of power switch on lightbulb in order to do that we're going to need a concept called an abstract class now this is as i said before not something that's built into python but python has a module that supports these abstract classes and with an abstract class you can specify what the interface should be that a class should adhere to so that it's kind of like a contract between the different parts of your program so let's create an abstract class that defines the interface for things that can be turned on and turned off so i'm going to write a class called switchable that defines this just like the lightbulb class the switchable class has two methods turn on and turn off there you go and for now these methods are not doing anything so i'm just going to write pass here so how do you turn this class into an abstract class for that you need to import the abc object from a module also called abc and to make switchable abstracts i simply inherit from the abc class so now switchable is an abstract base class that's what the abc stands for next to defining this abstract class you all should indicate that methods are abstract as well and for that we're going to use an abstract method annotation in order to use that annotation i also need to import it so because switchable is abstract i'm not allowed to create an instance of this class if i try to do that i get an error but what it can do is create subclasses of switchable so switchable in this example purely serves as a definition of the kind of methods that a class that inherits from switchable should have so light bulb for example i'd like to inherit from switchable there you go and now lightbulb already has these two methods so we don't have to change anything here but we're simply saying lightbulb is a subclass of switchable so it means that light bulb implements the interface that's defined in switchable so this doesn't actually change anything in the code in in what the program does so here you see i can still turn on and turn off the light bulb but for example if i forget to add the turn on method so let's put this into comments then you see that i get an error that i can't initiate this light bulb class because it has an abstract method turn on that is not implemented so these abstract methods they also help you to keep track of the things that you still need to implement now that lightbulb is a subclass of switchable we can change the power switch and this is where we're going to apply the dependency inversion principle so we're going to remove the dependency the direct dependency on the light bulb class and going to replace it with switchable and then obviously it's also a good idea to change the name here because this is no longer necessarily a light bulb it can also be something else so let's call this client and then obviously i also need to change that here now if i try to run this code again light bulb is still turned on and turned off so nothing actually changed except that we removed the dependency between the light bulb and the electric power switch we simply said we have a class that defines what it means that something is switchable well what does it mean it means that it has a turn on and a turn off method lightbulb implements this class so it is a switchable subclass the power switch is then dependent on switchable objects and no longer on light bulb because we removed the dependency on light bulb from the power switch we can also attach other objects to this power switch if you're enjoying this content by the way give this video a like it really helps support this channel so let's add another class let's say i i'd like to add a fan and a fan we can also fortunately switch on and switch off for testing this is just a simple print message again i'm not actually going to implement a fan obviously so we have a fan and now what's nice is i can create a fan here and then i can pass the fan to the switch and then when i press the switch the fan is powered on and off this is what you can achieve with dependency inversion it means that instead of having two classes that are directly coupled we now decouple them through an interface in this case called switchable as you just saw in the example dependency inversion helps you separate components it helps you reduce coupling in your code i made a separate video about coupling if you want to learn more about that there's a link in the top right whenever you're writing code components think about how they depend on each other and if you can use an abstraction to separate them in the long term this is going to help you write code that's really clean and easy to change and that's a good thing because software is always changing and this puts you in a situation where you're well prepared for any changes you need to make in the future i've put a few links to books and papers about solid design principles in the description below so check those out there's also a link to repository on github that contains the code example from this video so you can take a look at it and play around with it if you like that's it for today make sure to subscribe if you want to watch more of my content and let me know in the comments below what your tips are for writing better code thanks for watching take care and see you soon [Music] you
Info
Channel: ArjanCodes
Views: 21,734
Rating: 4.9921799 out of 5
Keywords: write better python code, dependency inversion principle, dependency injection, dependency inversion, solid design principles, visual studio, python tips, dependency inversion principle tutorial, dependency injection python, dependency inversion principle python, dependency inversion principle example, solid design principles python, python tips for beginners, python tips and tricks, python tips for competitive programming, Python tips and tricks advanced
Id: Kv5jhbSkqLE
Channel Id: undefined
Length: 9min 2sec (542 seconds)
Published: Thu Jan 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.