 Hi everyone, once again welcome to this class we are continuing our class on the programming class today we will continue our discussion on Python we were looking at several things basically like lot of basic constructs lot of data structures we looked at several things and in fact in the last lecture we also did some of the file Ios so how to open a file how to be the file how to close a file how to actually write into a file things like that we saw all the details regarding how to use them we also did lot of functions the built-in functions essentially and then we also kind of understood some of the concepts with underscore then the name underscore that is again another built-in functions that we can call into also in the last class we talked about lot of these importing Python scripts from some other location how do we import them how do we import a whole bunch of script bunch of functions and then how do we actually use a few of them things like that we saw like several options available in the Python language to support that so today we will shift our gears actually we will be covering couple of chapters the first chapter is going to be just one of the documentation we will talk about the comments dir the directory and then the bottom of the screen what are the different things that you can do here and then we will also like look at some of the object oriented functions that are available in Python this is mainly the class and the class libraries this is something that we will go through okay so without a lot of other intro let me go to the comments so comments have we actually saw this much earlier probably in the first class anything after a hash or character is a comment this can be basically on a line or at the beginning of the line wherever you put the hash and then type in something else that is treated as a comment so nothing special about it I mean so you can write comments everywhere urge you to write comments lot of comments it becomes readable for the next person who will support this the dir function essentially prints out all the attributes of an object so this object and attributes actually like study later and we talk about the class in the class libraries but just think about like I mean if you have some objects you can just use dir to report all its attributes so for example here we are importing this basically this is again the import functions that we studied in the last lecture and then when we say like dir sys it prints out all the various attributes for this this for example underscore display hook underscore underscore dot underscore except hook underscore underscore name sdr there are all those different functions that are there that that are all like display and then if you specify dir as a square bracket instead of this this and then basically there is a list so here it displays the attributes for the list which is add class contains append count extend index etc so all the list attributes are displayed when you say like they are list the square brackets denotes that it is a list now comes the doc strings the doc strings are nothing but documentation strings which are essentially used as documentation when we write our functions essentially strings at the top of the module files the top of functions and top of classes can all become doc strings which are automatically inserted into the underscore doc underscore attribute of the object so and then we can use regular single or double codes to delimit the string or we can also use triple codes for multi-use multi-line string so these are all like I mean the standard one so what we saw need to get to this okay so here as an example we have a function here which is basically some x y this we know like I mean how to define the function before following usually we only put this statement where we just return x plus y but in this case we will just add this this string here with the codes this function just adds two numbers and this becomes the doc string or this particular function so when we say like print some dot underscore doc underscore it prints just the doc string which is essentially like its attribute so this is the way to that that we can specify the doc string now there is there are also like doc strings available for built-in objects for example we can print these doc strings for built-in token objects by just using the relevant underscore doc underscore for example if we import says we can do print this underscore this dot underscore doc underscore so this is something that is useful so I think you can find out what this prints out now there is also a tool called pi doc the pi doc is a tool that can extract the doc strings and display them nicely either we are the health command or we are GUI or HTML interface so you can do like help list and then basically it will print out all those different doc strings and then for any of the documentation you can just go to the web and then search on www.piphone.org and you can get a lot of information from this website now we will go into the next topic which is essentially the classes and this is one of the big topics that we will be talking about today mainly like I mean we will first do an introduction to the classes what are classes and we will talk about how do we how can we do the what what what does it mean to actually have a class okay and this is again like I mean as I mentioned this is the object oriented functions so so we will talk about that and then we will also once we so we will introduce to the classes and then we will talk about stuff and junk okay so the class is essentially it is a user defined data type it contains certain member data which we will define what it is it also has associated things that it can do called member functions so anytime a class is defined should have a member data and if a class is a specialization or a subtype of a more general class it is set to inherit from that class and this is this can give a class access to the parents class member with data and functions so these are all the concepts essentially like I mean the object oriented functions first one is class so class as we saw it is basically it is a user defined data type the class will have some member functions and member data now a class can also have what are called subclasses which are nothing but specialized classes and these subclasses once you have they can inherit this the member function and member data plus its own its own specialized functions and specialized so this is the way to that you can actually look into what are classes and how the various functions the functions can be defined so the the member function and the member data that the subclass gets from its patient class that is called the inheritance or this is basically inherited class it is also called an inherited class and these pretty much these terminologies are all pretty much same as the C++ Java etc all like general concepts in other languages as well the classes are built into Python from the very beginning we actually like to use this term class many times before I hope you remember those things even though we did never talked about the object oriented programming itself and the object oriented programming basically that is the programming with using all these classes and the members member functions and the data the Python is superior to Perl in the sense okay so this is something that you may want to keep in mind Perl 5 is also object oriented which has all these things but Perl was developed as a non object oriented from beginning so it may not have all these things whereas Python has all the all the things that are already now how do we define a class the class is defined by two things one is the class name and a suite of code essentially or you can also define it as class of moon with the base classes and then a suite of code the class definition suite or the code block can contain member function definitions so you can define all the number functions within this what if this means is basically like whenever you define a data they can only they are restricted to these member functions and the member data the base classes go would contain any class that this class in a rich form now what is the member data scope or scope of the member data the class attributes that is the member data define the class definition itself or common to the class so the code block in the class definition actually runs the first time that the class definition is encounter and not when the class objects are just instantiated so this is another thing that you may want to keep in mind so when the class objects are instantiated the code block does not get run it only like it runs when the first time the class definition is in the class attributes define for a specific object object of a certain class are held only with that particular object often set using the self keyword so we will see an example later but again so this is basically once you define a specific object it only has it is held only with that particular object so when you go into another object or when you start define the object this property will also all already be gone so here is an example here we define a class called student the student has various attributes one is the course which is we define as m3 4 1 2 and then we define self function for first one is the underscore in it underscore so here we give like self name test 1 equal to 0 and test 2 some initial values so self name is the name that we assign self test 1 is just called test 1 some arbitrary function or self test 2 is also another test 2 now we can say like okay what is the compute average of self and then basically that will return basically it will add like test 1 plus test 2 divided by 2 and then you see the that average and then print data is essentially like I mean so you can ask it to print the grade for this particular course in this or grade for a particular name in this particular course is some the average of the complete average of test 1 and test 2 so if you have like multiple students so you can say like David is a student with attribute David as his name 90 and 100 whereas Bob is a student like the base class basically as a student class Bob the name and his test 1 and test 2 scores so now we can say like David print data and then it will print the grade for David in M 3 4 1 2 is 95 0% so it takes this compute the average and the output and for same thing for Bob in M 3 4 1 2 is 70.0 so the Dave dot print dot Dave dot print underscore data and Bob dot print dot data both of them generate these kind of results so some more comments on the student example the calling a function with the same name as the name of the class treats an object of that class I mean sorry create that object of the class the active function is called called is the underscore init underscore function within that particular class underscore init underscore is also known as the class constructor so whenever we talk about object oriented programming there are two things that you need to understand one is what is called a constructor and then the other one is called the destructor so the destructor will come the later on stage and each time a constructor is called a different object of that type is created these different objects will generally have different member data so one thing if you had noticed in the previous example basically the force name was this held constant it was the chem 3 4 1 2 class but however the the test the two test grades are specific to each student so the self keyword has the same role as this in C++ or Java it points to a specific instance of the class it is automatically supplied as the first argument to any class member so it is automatically supplied as a first class member first argument to any class member function and it must be used then referring to any member data or function of a particular class even if the member's data even if it is members data shared among all the instances of a class okay so now we come to what is known as the operator overloading this is again another important concept essentially where you can use the pre-existing functions and then overload them so that to suit your own customised so for example some certain operators like equal to W plus minus multiplication can be overloaded to work on a user defined data type as well as a built-in data types like this so today if you if to test if two objects are the same if they are integers we typically use the equal equal function on that operator but in if you are user defined data type and then you want to compare those two user defined data type see that a day match we can overload the the equal to operator and implement a function which can compare the two instances of the user so this is done by actually defining underscore eq underscore function for the class and let us see the example so here we define a class called point and then the point has in its underscore in it underscore basically it is the self x is 0 y 0 and then basically to turn self direction just for one now we can also define the equal to testing and basically in this underscore equal to underscore it is with self and other and then so we just basically say that return self dot x equal to other dot x so it basically does this testing and then reports the answer so here there is a caveat I mean that we will talk about so a equal to point nothing and then be equal to point 1 1 and then now it will print a so one thing that we have to do whenever we do the comparison we need to actually make sure that they are the two objects that are being compared are of the same type so we could be a little safer and make sure that the comparison is actually between the two objects of the same type and otherwise return a flag indicating that the comparison isn't implemented by our point class so if that is the case then how do we do it so here we just say basically if not in instance other point return not implemented else return self x equal to other x and so we can take the same functions and then basically like in close within this is then else if else to basically like test for whether the comparison is between the two objects of the same type or not now we don't have to stop at overloading the equal to sign we can also overload other operators comparison operators like less than the underscore LT underscore that is X the X less than Y or underscore LE underscore which is essentially stands for less than or equal to and then same thing for EQ which is equal to Y and then any is the non-equivalent or non-EQ which is essentially not equal to and then there is a GE and GT GE stands for the better than or equal to and then GT stands for just greater than so all the logical operators basically the testing of this we can overload and then use it so one thing that you may want to notice there is no overloading the call signatures methods in other languages like C plus plus you can overload the member functions to behave differently depending on how many arguments are passed to the function with Python does not work like that the last function defined with a given name is the one that will be used so the function itself we cannot overload in Python and then basically just uses the last defined function so it doesn't use the ones prior to that so a small example again here we have class a and then we define method and then we will say like self X but then we want to change it we have to just write a new function which is now here depth method is self X and Y so this just overrides the previous one and that is how so now we come to the inheritance essentially we talked about this a couple of slides ago so essentially a class can inherit from another another class allowing a class hierarchy if a class a inherits from class B then go class B is the super class of class a and state a automatically sorry class or a automatically has access to all the member data and the member function for class B so this is another thing that we briefly talked about so I just wanted to explain this some more again in a class a inherits from class B the class B is the superclass of class A and then class a gets all the the goodies from the class B basically all the functions are automatically access accessible by class a and if the class a redefine some some member function or member data which is also present in class B then the definition in a takes precedence over the class B so it will range now let us look at an inheritance example how to do the inheritance so here we define the class as class 1 and then on the score in it we defined as self the data equal to 0 and those are the starting conditions and then self that data is data so that data now we just print out the self of data and then we define a class 2 which is an inherited class of class 1 so see how we linked these two and then when we define a square so we can basically say like some data equal to some data times of data which is essentially like now start the multiplication so one thing to note here is essentially since we define the class 2 as class 1 it gets all these things essentially even the print or the display command is also there so we can specify like a s class 2 with 5 and then we can also specify a dot square as well as you know display so a dot square is defined here and then a dot display is inside so the first one is just a member function is class 2 and as a second function is an inherited from class so we saw like some of the methods so so let us now explore some of the alternative syntax for the method calls so instead of calling the methods through their objects we can also call them through their class name so here is an example so we define x as this class variable some x some class now we can call the method 1 as just x dot method 1 or some class dot method these both syntax are perfectly legal syntax now this alternate method may be useful if you need to guarantee that the super class constructor runs as well as the subclass construct so here is another example we define class 2 as inherited class from class 1 and then here we can define like underscore unit of the score for self data and then we can also like this to mention class 1 dot unit class 1 dot underscore unit dot underscore which directly refers to the one that is before that thing and then you can say like insert the class to code here so we can overload and array indexing syntax with underscore get so how do we do that we define a class for test it and then we say like define underscore get item is stealth n and then basically the return to stealth dot data and then the nth variable and now when we wanted to want to use this test it we can just say like a is equal to test it in our data equal to jump so now we get this violation now there are more to the use the system define function one such as get attribute and set attribute so the get attribute these functions catch the attribute references for a class so the get attribute catches all attempts to get the class attribute so this is example x dot name etc and then the set attribute catches all the attempts set a class attribute for example extra name equal to ball so we need to be careful with the set attribute because all these are stuff on the self attribute value become self underscore attribute underscore and then the value will be the attribute value and this can prevent one from writing code such as self dot attribute equal to value in the definition of the set so now how do we use the set attribute so setting the using the underscore a city or set attribute underscore so here is one of the example just say like class some class and then define underscore set attribute underscore equal to so this is basically like we capture the initial value and then when we say like self dot underscore tick underscore attribute equal to value then it will print the character and this avoids the syntax like self dot attribute equal to value which we cannot have in this definition now there is another member function underscore str underscore which is essentially it is meant to prevent a user friendly representation of a class of printing and then the other one is the underscore are here underscore that is meant to provide a string which would which could be interpreted as a code for reconstructing and class analysis so now here is one example so here the class called number we define the function this is lambda scoring underscore self data basically and then self data is the data and then we say REPR self and then the return the number with self data so now when you see this REPR essentially that is constructed as a string is interpreted as a code so here when we say this that is interpreted as a code so we say a equal to number five and then we say print a and then finally like the number is basically number five now I mentioned in the very beginning of this lecture basically the destructors essentially like along with that go with the construct so now let us look at what is a destructor the underscore Dell underscore is also known as the destructor which is called automatically whenever an extent an instance is being deleted so whenever the instance get blown off either due to capacity limitation or access the limitation so whenever the you want to delete something basically the structure is what is being called and essentially in Python it automatically does this garbage collection once that instance is being deleted so the garbage collection is basically there the spaces reclaim so I think like I mean you probably have heard about there are different types of memories the most common one or basically there are all the variables are all kept is called the heap okay so essentially like whenever you assign a variable or assign a value to variable essentially like I mean some portion of it is taken and then the particular is written there so when the heap gets filled up you cannot write anything more into the into the disk so when you remove something basically so every time like you allocate or you call the constructor which we saw earlier a portion of the memory is being allocated to this construct and then that is when like I mean the memory came on out everything is kept as a heap and it won't touch any other other program so basically other layer that we saw earlier very beginning like I mean also in the lectures like the programs are kept at one level the operating system is in another level and then there are other supervised functions will be kept in the level so whenever a constructor is called basically it gets allocated and allocated now the destructor basically comes automatically and then basically reclaims this memory back into the heap and that is the job of the destructor and then Python also normally cleans up the memory used by instance automatically so it is not necessary to put this in the destructor so there may be like less necessary and then in for example C++ so that is pretty much what I wanted to cover for today so we went through like the two important topics one form we just did some the documentation basically like the commands the dir command and then box strings then we went on to talk about the classes for Python so basically like those are essentially it's a user defined data type and essentially like it's like any other object oriented language so it has a member function and member data and then that is for the class and then you can also define sub classes which and you can inherit it it's basically it inherits all the properties of the class the original class so it also inherits the member data and the member functions and then you can add to it some specialized functions and specialized data and then we went through like some examples of the class and how it works okay I think that's pretty much all for today we will meet and start the CGI programming in the next class thank you