Python, Programming language The Magic Of Python
Now the point of this video is not to give you a full tutorial on all the stuff that i’m showing you, but just to expose you to what you can do when you really do have a mastery or kind of expert level understanding of a language. Now i don’t claim to be a python expert, but the stuff i’m showing you here is definitely expert or advanced level features in python and obviously you don’t need to understand every single aspect of python to understand this one kind of cool thing: i’m, going to show You here again, this is not meant to be a full tutorial just to expose you to what’s possible when you really have a deep understanding of a language and how you can implement some very advanced and interesting behavior that you may not have anticipated before so i’ll Kind of leave the intro at that, but i will mention that, if you’re interested in this type of stuff, you were able to follow along with this video, i do have an expert python tutorial series that goes through more things like this in the python programming language. I believe there’s six videos in that series and we’ll leave a link to that in the description and a card in the top right hand corner of the screen. So with that said, let’s look at the magic of python, but what is also magic is the sponsor of this video before we get started.
I need to thank the sponsor of this. Video, which is sublime merch sublime merge, is a git client that makes managing your code repository simple. Without hiding the power of git, it was created by the same team that made sublime text and was designed to write a fast and simple to use tool that still provides all the powerful gate features with line by line staging powerful search and easy customization sublime merge Offers a better and faster way to interact with your git repositories. One of my favorite features in this tool is the sublime highlighting that allows me to quickly see the changes in commits and provides proper syntax highlighting for over 40 supported languages out of the box. Another useful feature is that sublime merge, uses actual git and allows you to see all the commands it’s running for you and even add your own custom commands now. Just like sublime text sublime merge is fully customizable. You can change the layout theme and more sublime. Merge is free to evaluate with no time limits or restrictions and, if you’re a sublime text user already, you can bundle it with the newly released sublime text 4 and save get started with sublime merge today by clicking the link in the description, all right so let’s Go ahead and get into the video here now. The first thing i’m going to mention is why i called this video the magic of python, what is magic about python well in any programming language and python, especially there’s a lot of high level syntax and great features that you can use without having any understanding of How they actually work on the back end right and great example of of these sorry is functions like say, len right this len function we just take for granted that this can give us the length of any object.
We have functions like int. This can convert something to an entry, create a new int for us. We have a bunch of other functions right. We have the map function, we have the filter function, we have a ton of things that we can just use and we can kind of take for granted. We don’t have to understand how they’re implemented and when we use these things, some magic occurs in the backend, and then we get some result or some behavior, and so what i’m going to do here is kind of peel back. You know some of that back end code and show you what it is that’s actually going on in the back end. Specifically when you create a class now you’ll see why i’m going to show this to you in a second. But i want to start by kind of introducing the concept of objects in python to everyone now, i’m sure many of you are familiar with objects if you’re watching this video you’re, probably familiar with object, oriented programming, but one thing you have to realize about python is That pretty much everything is an object, so something like x equals one. You know this is an int data type, but if actually go ahead and print the type of x we can see. This is from class int. So what this really means is x is an instance of the class int. It just happens to have the value 1. now, since this is an instance of class int.
This means i can add, other integers to it right, i can say print x, plus 2 and that’s all good. We get the value 3. and if i am actually to print the type of x plus two. So if i do that, you see again, we get class int. The result here, which is three, is of type in so it’s an instance of the class it. So these classes are kind of like blueprints that define the behavior of the different objects in python. If i create another variable here, and we make this equal to a string – and i print the type of it well you’re going to see that it is just simply going to be string now, what happens, though, when i try to actually print say x, plus y? Well, when i do this notice, we get an error, it says: unsupported operand types for int and string. So since one of these variables is type string and the other is type ins, i cannot add them together, because that operation is not defined, and so just keep that in mind there that these classes or these types really are blueprints defining how objects can behave in Our program, what methods we can use on them? What operations or operators we can use with them it’s a blueprint defining the behavior that’s. Really, the key thing to think about classes are just blueprints that define how objects behave and you can obviously have many objects of the same type or of the same class.
So now, though, let’s create a class let’s go ahead and say class. We can just do a really simple class, maybe like person and we’ll define in a nit in here. We don’t need to do that. Say define underscore underscore in it underscore underscore and quite simply we’ll just take a name right and then we’ll say dot name is equal to name. Okay, so we’ve created this person class. This is great, and we now know that we can do something like x is equal to person, and then we can give it a name say like tim, if we run our program no problem, this works totally fine. However, why is it that we can write this syntax? How does python actually create a new class? That is a person class? Well, one thing i want to show you here is that if i type or sorry print the type of x, we get main.person. So the reason it’s saying main is because the module in which you run the python code in is called main. So whatever module is executed, its name is kind of main, and then we say dot person just to kind of signify that the person class is inside. Of the main module, that’s great that’s, pretty straightforward, i’m sure that’s, not confusing everyone. However, what happens when i look at the type of the class right so now notice, i’ve not created an instance of this class i’ll delete that i’m.
Looking at what the type of the actual class itself is, and when i do this notice, we get class type now that’s kind of strange, because when we think of creating a class, we think of creating a new type right, we’re, creating this person type. And we can create objects of type person, but it turns out the classes themselves are objects that are of the type type. So i understand that that’s confusing, but there is actually a blueprint above our object or above our class story that defines how we can create a class. So just like this is a blueprint for the person objects. There is actually a blueprint for creating classes just like there is a blueprint for creating functions right. If i define foo, maybe we take an x here and then we print x. I think many of you have probably seen this before, but i can print the type of foo oops and i see that this is class function. So there is a blueprint that allows me to define functions and there is a blueprint that allows me to define classes. Now, since there is this other type, this type type and notice we’re using this type function right here. This means that this is not actually the only way that we need to create a class and i’m going to show you now kind of the advanced behavior of classes and what you can do when you understand the fact that a class is of type type.
So let me erase all of this, and what i’m going to show you here is how i can create this exact same class without using this high level. Fancy syntax and the way we do this is we use something called type. So what i’m going to do is create some variable. Let’S just call this new underscore class. Okay – and this is going to be equal to type we’ve used this function before, but it turns out that this function cannot only give you the type of something it can allow you to create a new class of type type or a new object of type type, Which would be a class, so the three arguments that go to this function here are the following: they are the class name, so in this case my class name is going to be person any of the. What is a base class is sorry for this class. So anything this class is going to inherit from would go right inside of here and then all of the attributes on this class, so attributes of a class are things like methods, class methods, static methods, so on and so forth class variables. Those are all attributes of a class, and so what i’m going to do here is i’m going to create a function i’m going to call this function, person underscore a net. What this function is going to do is take in self and it’s going to say. Actually, sorry it’s going to take itself and it’s going to take a name and it’s going to say: self.
name is equal to name notice. This is the same as the init function right here. Okay, so we have this init function. What i’m going to do now is create an attribute of this class, which is the init method, okay, so i’m going to say, init, and then this is going to point to the person. Init now walk through this slowly in a minute, but what i’ve actually just done here with new class? This line right here has created this exact same person class. So sorry, it would be these three lines here, but these three lines are equivalent to this right here and i’ll. Show you what i mean in a second. So what this is actually done is it’s created a class called person, it has no base classes and it has one attribute, which is the init method, so double underscore init double underscore, and that is equal to this function right here, so you can imagine this is Me kind of mapping this init method to this function that we’ve defined, and now what i can do is i can actually use this new class variable to create an object of a type person. So let me actually just comment this out uh. What do i want to do here? Oops, i’m, in the wrong programming language, for that? Okay, so let’s just comment this out and what i’m going to do is say that x is equal to new class and then i’m going to type in the name of my person, i’m going to say 10 and then i’m just going to simply go here and Print and this will be x, dot name, okay, so when i do this you’re going to see that this works – and this is very strange – don’t get me wrong.
What we’ve done is we’ve stored the class person in the variable new class and then we’ve created an object of type person which we do by initializing new class and then we’ve looked at the name of that person. There you go now if i want to actually look at the name of this new class. What i can do is, i can print new underscore class dot, underscore underscore name underscore underscore oops and if i do this oops new class or any one more s here, you’re going to see that this new class variable is actually the person class. So hopefully, this isn’t confusing you too badly, but this is how you can actually create a class using this type function, and that is because again, there is a blueprint above our kind of pre built in classes here that tells you how to define a class now, Since that is the case, since there is this blueprint, we can do these very advanced things like this. Where we dynamically create a class, we can use this type function, we can make a class name, we can give any parent classes and then we can give all of these attributes. Now. Let me show you how you would add a method to this class, so we’ll leave these lines here. What i’m going to do is say, define, say name this is going to take in self and what this is going to do is print self.
name. Okay. So now, what i’m going to do is i’m, going to hook up this method here so i’m going to say: ok, the method say underscore name is equal to a function, and this is say underscore name now what i’ve done is i’ve allowed myself to use this Say: name method whenever i call dot, say name what that’s going to do is called this function right here, because i’ve kind of hooked them up in this way so now, rather than just printing x, dot name. What i can do is i don’t even need to say print. Actually, i can just say x, dot, say underscore name and then notice. It says tim. So that is how you would go about creating a method now we’ve already seen how you can add actual attributes or properties to this class, which is by simply uh creating the initialization sorry. So in the initialization right i have myself, i have my name and then what i could do after here is. I could take other attributes as well, maybe like age and i could say self dot. Age equals age, and these are kind of the instance variables associated with the class defined in this way. But the whole point of me showing this to you, is that this is exactly equivalent to me doing this in this class syntax and what happens in this class. Syntax is the python interpreter actually goes through parses. All of these methods, which are really just functions inside of the class right and then uses this type function, to create the class in the way in which we’re manually doing it down here.
So here, it’s, just kind of the you know short form syntax. Essentially this is the nice way the pretty way of doing things, but you can go right down to kind of the the bare bones level and create the class on your own. So if i wanted to make these classes equivalent, then i would need to do my method say name again. This would need to take self, and then i would need to just print self.name and now my person class here and my person class here are the exact same. So you might be asking me now. Why would i ever want to do that? Why did you just spend 10 minutes showing this? To me? This seems cool but it’s like relatively useless i’m. Never going to do this now chances. Are you probably never will do this right? This is something that’s very, very specific, and that is not a common thing to be doing in python programming and only very very advanced people actually kind of mess with this type of stuff. But the idea here is that this now allows you, with this knowledge, to actually implement much more advanced behavior in python and where this would lead to. If i were to continue going with this explanation, is something called metaclasses now i’m not going to explain metaclasses in this video, because i have an entire video going through metaclasses again, you can find that in the expert python tutorial series, but since we now know that The class person is of type type.
What that means is that i can create a class that actually is a subclass of class type and use that class to create new classes. So this is kind of weird, but i can make a class like class create class, and i can just put type here now. What i’m doing is i’m, actually subclassing type right i’m, creating a class that inherits from type and now what i can do is. I can change the behavior of the type class, some of it at least i can hook into and modify and overload different behavior, and i can actually change the way in which classes are created, and this means that now, when i want to create a new class, Rather than using type, i would use this create class thing. So if i just put a pass here – and i use create class like that, you’re going to see that this all works, the exact same because what i’ve done is i’ve created the blueprint for my class right i’ve, said: okay i’m going to have a new blueprint. Now it is a subclass of type, so, whatever type does this will do and then in here i could go in and i could change how type actually works and i can then put constraints on the modification of classes or the creation of classes, and i could Change how a class was actually constructed, what methods are called first is the init method called first is the call method called first? Is the new method called first? If you know what those are, if you don’t know what those are don’t worry, but it allows you to do very, very advanced and interesting things, so i think i’m going to leave the video at that i’m sure this last part was maybe a little bit of A brain explosion for some of you guys, because this is some pretty advanced stuff.
Let me know what you guys think of this. If you want to see more advanced, syntax and concepts in python, do let me know i usually stay away from them just because i know a lot of you guys are beginners or intermediate programmers, but if there’s a demand of course, i’m happy to make some more Videos about this topic, regardless, i hope you guys enjoyed if you did make sure to leave a like subscribe to the channel and again you can check out that expert python.