The Magic of Python - : Latest Jobs In Pakistan 2022

Thursday 22 July 2021

The Magic of Python

Hello everybody and welcome to another article. So in today's article, I'm going to be sharing with you some very advanced and interesting programming concepts in the Python programming language. Now, the point of this video is not to give you a full tutorial on all of this 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 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 on that, but I will mention that if you're interested in this type of stuff, you are 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 well I'll leave a link to that in the description and a card in the top right-hand corner of the screen. So 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 merge. 

Sublime merge is a get client that makes managing your code repository simple without hiding the power of get it was created by the same team, the made sublime text and was designed to write a fast and simple to use tool that still provides all the powerful gift features with line-by-line staging, powerful search and easy customization sublime merge offers a better and faster way to interact with your get repositories. One of my favorite features in this tool is the sublime highlighting. It 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 grit 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 for 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 will 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 backend. Right? And great example of, uh, 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 into this can convert something to an intro, create a new end 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 implement and when we use these things, some magic occurs in the back end, and then we get some results or some behavior. And so what I'm going to do here is kind of peel back some of that backend code and show you what it is that's actually going on in the backend 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 inter data type, but actually go ahead and print the type of X. We can see this as from class it, so what this really means is X is an instance of the class.

It just happened to have the value one. Now, since this is an instance of class, and this means I can add other introduced to it, right? I can say print X plus two, and that's all good. We get the value three. And if I am actually to print any type of X plus two. So if I do that, you see again, we get class in the result here, which is three is of type in. So it's an instance of the class. 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 and it says unsupported, opera and types for int and string. 

So since one of these variables is type string and the other is type in, I cannot add them together because that operation is not defined. And so just keep that in mind there, 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, uh, 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 knit in here. We don't need to do that. 

Say define underscoring, a square knit, underscore, underscore, and quite simply, it will just take a name, right? And then we'll say 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, 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 Syntex? 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 Surrey print the type of X, we get main dot person. So the reason it's saying Maine is because the module in which you run the pipe and coding 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 classes inside of the main module, that's great. That's pretty straight forward. 

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 class, we think of creating a new type, right? We're creating this person type, and then 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 Fu 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 Fu oops. And I see that this is class function. So there is a blueprint that allows me to define functions. And there's 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. 

Some of you erase all of this. Now what I'm going to show you here is how it 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 can not 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, a, what is a base class classes, 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 tribute 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 in self and it's going to take a name and it's going to say, 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 a knit. And then this is going to point to the person a knit. 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. What's more, I'll show you what I mean in a second. So what this has actually done is it's created a class called person. It hasn't no base classes and it has one attribute, which is the init method. So double underscore and knit, 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. So let me in reality remark this out. Uh, what do I want to do here? Oops. I'm in the wrong programming language for that. Okay. So let 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 Okay. So when I do this, you're going to see that this works and this is very strange. Don't get me wrong. But 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, uh, oops, new class or any one more S here, you're going to see that this new class variable is actually B 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. That is because again, there is a blueprint above our kind of prebuilt 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. So we'll leave these lines here. And 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 Frindt self darn it. 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, okay, 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 same name method. Whenever I call dot Saint 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 Exxon 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 tip. 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, um, creating the initialization. Sorry. So in the initialization, right, I have myself, I have my name and then what I could do after years, I could take other attributes as well, maybe like age. And I could say self dot age equals age and needs to kind of the instance variables associated with the class to find 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 just kind of the short form syntax, essentially, this is the nice way, the, the way of doing things, but you can go right down to the, uh, the bare bones level and create the class on your own. 

So if I wanted to make these costs equivalent, then I would need to do my methods. Say name again, this we need to take self. And then I would need to just print 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. No 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. 

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 meta classes. Now I'm not going to explain medic classes in this video because I have an entire video going through medic classes. 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 time and use that class to create new classes. So this is kind of a weird, but yeah, 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 sub classing type, right? I'm creating a class that inherits from type in, 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 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 as 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 bat. I'm sure this last part was maybe a little bit of a, a brain explosion for some of you guys, because this is some pretty advanced stuff. Tell me your opinion about this. If you want to see more advanced syntax and concepts in Python, do let me know. I usually stay away from them just cause 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.

No comments:

Post a Comment