1. Home
  2. Explore
  3. Computer Science
  4. Python OOP Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python OOP Course

Transcript

It's important for software developers to  understand object oriented programming.   In this course, Jim from JimShapeCoding will teach  you all about object oriented programming and   Python object oriented programming, it could be  what is holding you back from being a great Python   developer. And as well as lending your first job  as a software engineer, Welcome everyone to Python   object oriented programming course. Now if you  struggle to understand the concepts of object oriented programming in the past, then you are  totally fine. And you're in good hands. Because   in this course, I'm going to make sure that this  will be the last tutorial that you will ever watch   about classes and the complex concepts that  comes with object oriented programming. And we   are going to do this by developing a real Python  application, that is going to be very cool to   write. And we will add to its complexity step by  step. And throughout the way, we will understand

everything that we need to know about object  oriented programming. Now there are going to   be some requirements to this course, I do expect  from everybody to know at least about functions,   variables, if statements and as well as for  loops. And if you know those things from   other programming languages, then this  is also fine. So with that being said,   let's get started. Now to explain why you  should write object oriented programs, I will explain the concepts based on a store  management system that we will start developing   together. So starting to think about how to  take our first steps with such a problem,   we could first think about tracking after the  items that we have right now in our store. So one   way we could get started, we could create those  four variables to start tracking after our items.   So as you can see, we have our first variable item  one equals to phone. And then we have three more

variables that are intentionally starting with the  prefix of item one, so that we could describe that   those four variables are related to each other  by following the correct naming conventions. Now,   you might think that those four variables are  related to each other only because it uses the   same prefix of item one. For Python, those are  just four variables with different data types.   So if we were to print the type, for each of those  four variables, now, we will receive their types with no surprises, right, we will receive string,  and integer for price, quantity and price total.   Now I want to focus on those specific outputs  right now, because as you can see, for each of the   types, we also see the key word of class. Now this  means that those data types are actually instances   of strings or integers. So in Python programming  language, each data type is an object   that has been instantiated earlier by some  class. And for the item, one variable that has

been instantiated from a string type of class.  And for the price quantity and price total,   those have been instantiated from a class that  is named Iand, meaning integer. So it could   have been nicer. If we call the tail Python  that we want to create a datatype of our own,   it will allow us to write a code that we can  reuse in the future easily if needed. Now,   each instance could have attributes to  describe related information about it. And we can think about at least some good  candidates for attributes we could have for our   item datatype, like its name, price, or quantity.  Alright, so let's go ahead and start creating our   first class. So I will clean everything from here,  and we'll go ahead with it. So it is going to be   divided into two parts, the first one will be the  creation of the class. And the second one will be

the part that I will instantiate some objects of  this class. Now when I say creating an instance,   or creating an object, basically I mean to the  same thing, so you might hear me saying one of   those. Alright, so let's go ahead and say class.  And then this needs to be followed by the name   of the class that you want to create. So we would  like to give it the name of item. And then inside   of this class, in the future, we are going to  write some code that will be very beneficial and very useful for us. So we won't repeat ourselves  every time that we like to take similar actions.   But for now, temporarily, I'm going to say here a  pass so we will not receive any arrows inside this   class definition. Alright, so now that we have  created our class, then we are allowed to create   some instances of this class. So let's go ahead  and say item one is equal to item. And that action

is equivalent to creating an instance of a class,  just like if you were to create a random string,   then you will say something like the following.  This is equivalent to this one as well. So it   is very important to understand how classes are  working in Python. So I will delete this line   because this was just for an example. And now I  said that we are allowed to assign some attributes   to instances of a class. So let's go ahead and  start creating attributes. And that will be

achievable by using the dot sign right after the  instance of a class. And here you can say that you   want to give it an attribute, like a name, that  will be equal to phone, and item one, that price   could be equal to 100. And I think one dot  quantity could be equal to five, for example.   Now in that stage, you might ask yourself, what  is the difference between the random variables   that we have created to those four lines?  Well, here, we actually have a relationship

between those four lines, because each one of the  attributes are assigned to one instance of the   class. And I could probably do this by going ahead  and try to print the types of item one nil, and as   well as the types of the attributes of name, price  and quantity. Now with name, price and quantity,   we are not going to have any surprises because we  assign string type attributes to the item object.   But if we were to print that, then check  out the result if I was to run this program,

so you can see that now we have a data type of  item here. And that is the big difference between   what we have seen previously to this thing that  we have just created. So now we understand how   we can create our own data types. Now let's go  ahead and see what are the rest of the benefits   using object oriented programming. Okay, so until  now, we understood how to assign attributes to   instances, we should also understand now how we  can create some methods and execute them on our instances. Now, if we will take as an example, the  building class of string, then you know that we   have some methods that we can go ahead and execute  for each of our strings. And for this example,   you can see that I grabbed an instance of a string  that I named random str, and then I go ahead in   the next line and execute the opera method, which  if you remember is it's possible to grab all

the letters and turn them to uppercase. Now the  biggest question here is how we can go ahead and   design some methods that are going to  be allowed to execute on our instances,   Well, the answer is inside our class. So we could  go inside our class and write some methods that   will be accessible from our instances. So we could  go ahead and say that and give our method a name.   Now a good candidate for a metal that we'd like  to create now is actually calculate total price,

because as we understand, it could have been  nice. If we were to have a method that will go   ahead and calculate the result, multiplying it one  dot price, with item one dot quantity, so we can   get the total price for that specific item. Now  before we go ahead and complete this function,   then I'm going to just create one more instance  of this item by also deleting those two lines,   because we understood the example. So I'm  just going to change those to item two, like that. And I'm going to use something like  laptop and change the price to 1000. And say   that we have three of those. Now just a quick  side note, when you will hear me say methods,   then I basically mean two functions that are  inside the classes. Because in terms of Python,   or in any programming language, when you have  isolated definitions with this keyword, then those   are considered to be called functions. But when  you go ahead and create those functions inside

classes, then those are called methods. So that  is an important point that you should understand,   because I'm going to call those methods  from now. Okay, so now if I was to continue   by opening up and closing those parentheses,  then you are going to see one parameter that   is autogenerated that Python wants us to receive  intentionally. Now the reason that this happens,   Python passes the object itself as a first  argument, when you go ahead and call those

methods. Now, if I was to go here, and  say item one dot calculate total price,   then the action that we are doing now is calling  this method. But when you go ahead and call a   method from an instance, then Python passes the  object itself as the first argument every time.   So that is why we are not allowed to create  methods that will never receive parameters.   Now you will see this if I was to remove the first  parameter, and say something like pass. Now if I

was to execute this program now, then you're going  to see type zero, calculate total price takes   zero positional arguments, but one was given.  So in simple words, what this exception says   is that Python tries to pass one argument and you  are not received using any parameter, so that is   very problematic. And that is why you always have  to receive at least one parameter when you go   ahead and create your methods. Now since we always  receive this parameter, then it is just a common

approach to call this self. It was okay if I was  to call it something like my perm, or I don't know   something else. But you never want to mess up  with common conventions across different Python   developers. So that is why just make sure that  you leave it as self every time. Now, if I was to   go ahead and run this program, then you got to see  that we are not going to receive any errors. So   this means that this method has been implemented  correctly. Now let's see how we are going to benefit from creating this method, because it  should go ahead and create a calculation for us   using price and quantity. So I will intentionally  receive here two more parameters, which we could   name just x&y for now. And we could just  say return x multiplied by y. And now I   will go ahead and pass in here, two additional  arguments. And it will be item one dot price.

The second one will be quantity. So that is  going to work because when you call this method   in the background, Python passes this as an  argument. And then it passes the second argument.   And then this has been passed as a third argument.  So that is perfect. And if I was to run that,   and actually print this, so excuse me for running  this before printing it, so I will surround this expression with this print built in function.  And I will run that and you're gonna see 500 as   expected, now I could do the exact same thing for  calculating the total price of our second item.   So if I was to grab this and paste this in,  in this line, and actually change this to item   two, and change this one to item two, and  as well as this one, then I will receive

3000 as expected. And that is how you can  create a metal. Alright, so until that point,   we understood that we can assign attributes and as  well as creating some methods that we can go ahead   and use them from our instances directly, like  those two examples in that line, and as well as in   that line. Now in that episode, we are going to  solve some more problems that we have in terms of   best practices in object oriented programming,  and things that you're going to see in each project that is based on Opie. Alright, so let's  get started. Now one of the first problems that   we have here is the fact that we don't have a set  of rules for the attributes that you would like to   pass in in order to instantiate an instance  successfully. And what that means, it means   that for each item that I want to go ahead and  create, I need to hard code in the attribute name   like those in here. And it could have been nicer  if we call somehow declaring the class that in

order to instantiate an instance successfully,  name, price and quantity must be passed,   otherwise, the instance could not have been  created successfully. So what that means, it means   that it could have been a great option if we could  somehow execute something in the background. The   second that we instantiate an instance and there  is a way that you can reach such a behavior. And   that is by creating a special method with a very  unique name, which is called double underscore init double underscore. Now you might hear this  term as well as cold as constructor. Basically,   that is a method with a unique name that you need  to call it the way it is intentionally in order to   use its special futures. Now, the way that this is  going to work is by creating it the following way.   So it will be double underscore. And as you can  see, I already have auto completion for some very

special methods that are starting and ending with  double underscore. Now the collection of those   methods are used to be called Magic methods. And  we are going to learn a lot of more magic methods   that you have in Opie, but the first one that we  are going to start with will be the init double   underscore, like that. Alright, so now that we  have created this method, then let's actually see   what this metal does in the background. So when  you go ahead and create an instance of a class, then Python executes this double underscore  init function automatically. So what that means,   it means that now that we have declared our  class, Python is going to run through this line.   And then since an instance has been created, and  we have double underscore init method designed,   then it is going to call the actions that  are inside this double underscore init   double underscore method. Now in order to prove  that, then I'm going to start with a very basic

point here that will say I am created Like that.  Now we got here, one instance. And here we got   another one. So we should see I am created twice.  And in order to avoid confusions, then I'm going   to delete those print lines from here so we can  see a cleaner picture. Alright, so if we were to   run our program, then we can see that we have I am  created twice. And that is because Python called this double underscore init double underscore  method twice, thanks to those two instances that   we have graded. Alright, so now that we use the  double underscore init function in this class,   we should take benefit from it and solve some  more problems in order to implement Opie best   practices. Now if you remember in the beginning  of this tutorial, I said that one of the problems   that we have till this point is the fact that we  still hard code in the attributes in that way by

saying dot name, dot price dot quantity. And  that is something that we can for sure avoid.   Now let's see how we can start avoiding creating  those attributes hard coded for each of the   instances here. So we can actually benefit from  the double underscore init method that we have   designed. And let's see how now we understand  that for each instance that we will create, it   will go ahead and call this double underscore init  method automatically. So what that means, it means that not only we can allow ourselves to receive  the self parameter, because this is a mandatory   thing that we should do, because Python in the  background, passes the instance itself as the   first argument, we could, in addition, take some  more parameters, and then do something with them.   So as a solder, let's say that we would like to  receive one more parameter that we could name it   name. And as you can see, automatically, Python  is going to complain how the name argument is

not filled in here. So now, I could go ahead and  pass in the argument of phone for that one. And   for the second one, I can go ahead and pass in the  argument of laptop. Now once I have created this,   then I can actually go ahead and change my  print line a little bit. So it will be a unique   print line where I can identify from where each  print line came from. So I can go ahead and say   an instance created and use a column here and  then refer to the name like that. And now that

we have created this, then if we were to run our  program, then you're gonna see unique sentences,   an instance created for the phone, and as well as  for the laptop. Alright, so now that we have done   this, then there is something that is still  not quite perfect, because we still pass in   the attribute of name here and here. So now pay  attention to how the init method has to receive   the self as a parameter as well. And we already  know the reasons for that. And the fact that we

have self as a parameter here could actually allow  us to assign the attributes from the init method,   so that we will not have to go ahead and assign  the attribute of name for each of the instances   we create. So what that means, it means that  I can dynamically assign an attribute to an   instance from this magic method, which is called  double underscore in it. So if I was to say, self,   dot name, so I'm assigning the  attribute of name to each instance,

that is going to be created or created yet,  and I'm making that to be equal to the name   that is passed in from here. So what that means,  it means that now I can allow myself to delete   this line. And then this line. So as you can  see, now I have a dynamic attribute assignment,   thanks to the self dot name equals name that  we have wrote here in the to test that the   attribute assignment world, then I can go down  here and use two more lines that will look like

the following. So I will print it one dot name,  and I will also print item to that name. And in   order to avoid confusions, then I'm going to  get rid of this line. So we could only see   the print lines from here. And now if I  was to run that, then you can see that we   receive a phone and laptop. So it means that we  were able to assign the attributes dynamically.   And that is perfect. And now that we get the idea  of that, then we should also do the same for the rest of the attributes that we'd like to receive.  So we also got the price and quantity to take   care of. So I'm going to go to my init  method, and I'm going to receive again,   price and quantity. And I'm going to do the exact  same thing. So I'm going to assign the attribute   of price. And that will be equal to price. And the  quantity will be equal to the quantity. And you   can also see that again Python complains about the  price and the quantity not being passed in here.

So I can say 100 and then five, and then I can  delete those. And then I can do the same here.   I could pass In 1000, and then three, and delete  those, and in order to prove that this is going   to work, then I'm going to copy myself a couple of  times and change this to quantity, I mean price, this one will be price as well. This one will be  quantity and this one as well. Now if I was to   run that, then you can see that the results are  as expected. So that is a way that you should   work with the double underscore init method,  you should always take care of the attributes   that you want to assign to an object inside the  double underscore init method meaning inside the   constructor. Now a couple of signs that are quite  important to remember when we work with classes.

Now when we go ahead and use the Ws coordinate  method, this doesn't mean that we cannot   differentiate between mandatory parameters to non  mandatory parameters. So say that you currently   don't know how much you have from a specific  item, then you can go ahead and by default,   received this quantity parameter as zero, because  it is realistic situation that you currently don't   know how much phones you have on your store.  so we can directly go ahead and use a default value for that, for example, zero, and then this  will mean that you will not have to pass in those   five and three here. And now in order to show you  the results of that, if I was to run our program,   then you can see that we receive zero twice for  those two prints in here. So that is something   that you will want to remember. And one more quite  important point that I'd like to talk about now   is the fact that you can assign attributes to  specific instances individually. So say that

you want to know if the laptop has numpad are not  because some laptops are not having the numpad on   the right side of the keyboard. But this is not a  realistic attribute that you will want to assign   to a phone. And that is why you can go ahead  and let me delete those print lines, by the way.   And that is why you can go ahead and say something  like item two that has numpad equals to false like   that. And that is something that you want to  remember, because the fact that you use some

attribute assignments in the constructor doesn't  mean that you cannot add some more attributes that   you will like after you instantiate the instances  that you would like to. Alright, so now that we   understood this, then there is still one small  problem that is left that we need to solve.   Now pay attention how the calculate total  price still receives the x and y as parameters.   And the question that we asked now is why it still  receives those parameters. Well, we could for sure

now not received those parameters. Because as we  know, for each metal that we design in classes,   then the object itself is passed in argument. And  I know that I repeated this a couple of times.   But this is where I failed to understand classes.  So that is why it is very important to understand   this behavior. And we already know that the  object itself passed as an argument. So that's   why we receive self. And so this means that now  we could just return self dot price multiplied

by self dot quantity. And this will mean that we  don't really have to receive those parameters,   because we assign those attributes, once the  instances has been graded. So this means that   we have access to those attributes through how  the methods that we are going to add here in   this class in the future. So in order to  test that this works, then I'm going to   delete this example for now. And I'm going to say  print item one dot, calculate total price. So we will be able to return the result here. And I will  do the same for item two, sorry, only this one.   Now to show some real number other than zero, then  I will go ahead and pass in here, quantities. So I   will say one and three, for example, because I  don't want to multiply a large number which is   zero. And that could come from here. So I will run  that. And you'll see that we receive the expected

results. So now we completely understand the big  picture, how to work with the constructors in   classes, and what are the best practices that  you should go ahead and implement. Alright,   so now that we understood this, then we might  think that we have done everything perfectly.   But actually I want to show you what will happen  if we were to pass in here a string besides   an integer and run our program. So if we were to  run that, then you can see that we are screwing things up here. Because this function for example,  things that he chose to print the string three   times because you'll see we have 1000 multiply  by three that is being returned in here. So it   shows us 1000 once, 1000 twice, and then one more  time. So what that means, it means that we have   to validate the datatypes of the values that we  are passing in. So there are a couple of ways to

achieve this. And one way is by using typing's in  the parameters that you're declaring inside here,   so a great starter will be, for example, to  declare that a name must be a string. Now, let me   first take this back and change those to integer  and then go here and design those parameters.   So in order to specify a typing, then you should  go ahead and create a colon sign, followed by the type of the datatype that you expect to receive  here. So if I was to pass in here, only the object   reference to the class of str, then it will mean  that it will have to accept strings only. And I   can prove that by changing this to an integer. And  you're going to see that we have a complaint here   that says expected type str God int instead. And  that is perfect. So now that we have done this,   then I'm going to do the same for the price  itself. And the price, we could actually do the

same thing with it by passing in float. Now when  we pass float, it is okay to also pass integers.   And that is something very unique with floats  and integers together. So that is okay to   use the typing of float. And for the quantity, we  don't need to specify a typing, because the fact   that we passed a default value of integer already  marked these parameter as to be integer always. So   that is why, for example, if I was to leave this  as it is and change the quantity to a string, then

you're gonna see that it is going to complain,  because the default value is already an integer.   So it expects for an integer. All right, so those  things are actually great setups to make our init   function more powerful. But we might still want to  validate the received values in the following way.   So say that you never want to receive a negative  number of quantity. And you'll never want to   receive a negative number of price. So that  is something that you cannot achieve by the typing's in here. But there is actually a great  way to work this around. And that will be by using   assert statements. Now assert is a statement  keyword that is used to check if there is a match   between what is happening to your expectations. So  let's see how we can get work with assert. So I'm   actually going to delete this from here. And I'm  going to organize our init method a little bit,   I'm going to say here a comment and I will say  assign to self object. And I will say up top

something like run validations to the received  arguments. Alright, so now it is a great idea   to validate that the price and quantity are both  greater than or equal to zero, because we probably   don't want to handle with those when they are  negative numbers and we want to crash the problem.   So we could say assert and pay attention that  I use it as a statement not a built in function   or something like that. And I can say here,  price is greater than or equal to zero. Now once I said this, then I can also do the same for  quantity, actually. So let me do that quickly.

By this way, and then once we have this, then  I can actually go ahead and run our program.   And you will see that I will not receive any  arrows. But the second that I change this quantity   to negative one, for example, and this one being  negative three, then I will have some arrows   that will say, assertion error. Now you can see  that the fact that we see here, assertion error   is quite a general exception, that doesn't  mean anything. Now what is so beautiful with

a third, you can add your own exception messages  right near of it as a second argument. So let's go   up top here and go back to those two lines. So the  first argument that is passed to the statement is   the statement that we'd like to check. But if we  were to say here comma, and use a string to say,   actually formatted string, and I can say price and  then refer to the value of it is not greater than zero like that. They can add an explanation  mark here, and they can use the same thing.   Copy that with a comma and paste this  in here. And changed this quantity

and then refer to the value of it and say that  it is not equal to i mean greater than or equal   to zero. So we need to be actually changed  to greater than or equal to, like that.   And same goes for here, and I have some a  space here that will be deleted. All right,   so now if I was to execute our program, then you  can see that we receive assertion error quantity   minus one is not greater or equal than zero.  So I should delete this, then here for that,

and now it is perfect. So now we understand  that using the assert statement could allow   us to validate the arguments that we receive.  And also, it allows us to catch up the bugs as   soon as possible, before going forward with  the rest of the actions that we'd like to take   within this program. So let me actually change  those back to valid values like that. And   that is perfect. Alright, so until this point, we  learned about how to work with the constructor. And we also learned about how to assign different  attributes to instances that are going to be   unique per instance, which means that you can go  ahead and create as much as instances as you want,   and you have the control to pass whatever  values you would like to for the name,   price and quantity. Now consider a situation that  you'll want to make use of an attribute that is   going to be global, or across all the instances  now are a good candidate, for example of this

could be a situation that you will want to apply a  sale on your shop. So this means that you want to   go ahead and having the control of applying some  discount for each one of the items. And that is   a good candidate for creating an attribute that  is going to be shared across all the instances.   Now we call those kinds of attributes, class  attributes, and the kinds of attributes that we   have learned that till this point is actually  called in a full name instance attributes. So about instance attributes, we know  everything, and we learned how to work with it,   but we did not work it with the other kind of the  attributes, which we will do in this tutorial,   which is called again, a class attribute.  So a class attribute is an attribute that is   going to be belong to the class itself. But  however, you can also access this attribute   from the instance level as well. Let's go ahead  and see a good candidate for a class attribute

that you want to go ahead and create it. So  that's going to be going to our class here.   And just in the first line inside our class,  I can go ahead and create a class attribute.   So let's go ahead and create an attribute like  pay rate equals to 0.8. And the reason that I'm   doing this is because I said that there is going  to be 20% of discount. So I probably want to store   an attribute that will describe how much I still  need to pay. So I will say here, the pay grade after 20% discount like that. Okay, so now that  we have created this, then let's see what are the   ways that we can access this attribute. Now, if I  was to go down and actually deleting one of those,   and say something inside this print line that will  look like the following. So I will try to access   to the reference of the class itself. So I'm not  going to create an instance like that, besides,

I'm just going to bring in the reference to the  class level itself. And I'm going to try to access   this attribute by saying the PE underscore rate.  Now if I was to run that, then you're going to see   that as expected, we see this class attribute,  because that is a way that you can access   those class attributes. Now this might be  confusing, but I said a minute ago that you   can also access those class attributes from the  instance level. Well, let's see if that is true. So if I was to duplicate those lines twice, by  using the shortcut of Ctrl D, then let's go ahead   and change those to item one, and this one to  item two. Now see how I try to access the pay rate   attribute from the instance, although we don't  have such an instance attribute. Now if I was to   run that, then you're going to see that we still  have the access to see that class attribute. Well,   that might be confusing. And that might be  hard to understand why that is happening.

Well, there is actually something that we need to  understand when we work with instances in Python.   So when we have an instance on our hand, then At  first this instance tries to bring the attribute   from the instance level at first stage, but if it  doesn't find it there, then it is going to try to   bring that attribute from the class level. So what  that means it means that item one did something in here and say to itself, okay, so I don't have this  attribute right in here because that is just not   an attribute that assigned to me. So I'm going  to try to search that from the instance level   and then I'm going to find it and if sprinted  back. So that is exactly what is happening here.   Item one and item two are instances that could not  find the pay rate attribute on the instance level.   So both of them went ahead and try to bring  this attribute from the class level. And since

it really exists in the class level, then we  were able to access those. Now to even give   you a better idea of what is going on here. Then  I'm going to do one more additional thing. Now I   will delete these first print line. And I will  go ahead and delete those attributes from here   as well. Now there is a built in magic attribute,  not a magic method, that you can go ahead and see   all the attributes that are belonging to that  specific object. And that is achievable by using this double underscore vi CT double underscore  like that. So this will go ahead and try to   bring you all the attributes that are belonging  to the object that you apply this attribute and   want to see its content. So I will go ahead and  copy this one and paste this in for the instance   level as well. So this will give me all the  attributes for class level. And the second line   will do this for the instance level.  Alright, and if I was to run that,

then let's explore the results for a second. Now  we can see that at the first line, we see this   pay rate attribute. But in the second line, we  never see it, we see name, price and quantity.   And you can also pay attention that this magic  attribute is actually responsible to take all the   attributes and convert this to a dictionary. And  that is from where the dict keyword coming from   it is just a shortened version of a dictionary.  So that is a very useful magic attribute that you can go ahead and use if you just want  to see temporarily for debugging reasons,   all the attributes that are belonging to some  object. Alright, so now that we understood this,   then let's take it to a real life example and  come up with a method that will go ahead and apply   a discount on our items price. So that will be by  creating a method that will we belong to each of   our instances in that means that we can go ahead  and come up with a method that we could name apply

discount. So let's go ahead and start working  on this. So I'm going to say def apply, discount   and pay attention that I'm using a new method  inside a class here. So right inside of this,   then add first we need to figure out how we are  going to override an attribute that is belonging   to an instance. And we already know that we can  do that with the self keyword. So it will be   self dot price. And that will be equal to self dot  price, meaning the older value of this attribute

multiplied by the pay rate. Now you might expect  that we could access this directly like that. But   if you remember, that is actually belonging to the  item class itself. Now this might be confusing,   because this method already inside this class.  So you might think already that you can access   it directly by saying pay rate, because it is  already inside the class. But that is actually not   going to work. Because you can either access it  from the class level or the instance level as we understood previously. So we can go ahead and say  item dot pay rate like that. And there you have a   metal that can go ahead and basically override  the price attribute for one of your items.   Now to show you that this works, then I can only  use one instance for now. And I can go ahead and   call this method by saying apply discount. And I  can also now try to print the attribute of price   for this item one, and we should see ad right. So  if we were to run that, then you're going to see

that we are going to receive at point zero as  expected. Now we should not forget the option   that you might also want to have a different  discount amount for a specific item. So say that   one day you will have 20 items or in only for the  laptop, you will want to have a 30% discount. But   it is going to be a bad idea changing the class  attribute to 0.7 because it will affect all the   items that you have right now on your hand. So  what you can do instead is you can assign this attribute directly to one of the instances that  you would like to have a different discount amount   for so let's go ahead and see an example for  this. So I will allow myself to bring back the   item or laptop and then what I can do to apply  a 30% discount for this item is assigning the   exact same attribute to the instance. So I can go  ahead and use a item to that pay on the score rate   is equal to 0.7. Now what will happen here is  that for item two, it will find the attribute

of pay rate in the instance level. So it does  not really have to go ahead to the class level   and bring back the value of pay rate because  Add first look, it is going to find it in the   instance level. But for item one, it is different,  it is still going to read from the item level,   which is going to be 0.8. So now, if we were  to try to use item two dot apply discount,   and as well as printing the price now, then let's  see what will happen. So I will uncomment this

line to not see this screen for now. And I will  go ahead and execute our program. Now you can see   that we still, however, receive 800. And what this  means this means that the discount that has been   applied is still a 20%. And where this is coming  from, well, this is coming from this method here   that no matter what we try to pull the pay rate  from the class level. So a best practice here will be to change these two cells. And that way, if  we override the pay rate for the instance level,   then it is going to read from the instance level.  But for item one, if we try to access the pay rate   from the instance level, then this is still great,  because we did not assign a specific pay rate for   item one. So it is going to pull that from the  class level. Now if we were to try to run that,   then you're gonna see now that we have expected  results. And if we were to also uncomment,

the first print line for the item one and rerun  our program, then you can see that for item one,   we had 20% discount. And for item two, we had  30% discount. So when it comes to accessing   class attributes, you might want to reconsider  how you want to access them when you will come   up with some methods. And specifically  for creating a method like apply discount,   it is a great idea to access it from the instance  level. So you also allow the option of using a pay rate that is assigned to the instance level.  Okay, so now that we understood completely   about the differences between a class to an  instance attribute, let's jump ahead to the   next topic. Now you'll see that I have deleted  those print lines that I have down below.   And I came up with five instances that I have  created here. So you might also want to create   those five instances immediately. So that is why  I will recommend you to go here to my repository,

accessing these class attributes directory, and  then code snippets, and then go ahead and copy   the code from these five underscore items.py file.  Okay, so considering a situation that your shop is   going to be larger in the future, meaning that you  are going to have more items, then the more items   that you're going to have the more filtration  like things that you want to do in the future. But what is problematic currently with our class  is the fact that we don't have any resource where   we can just access all the items that we have in  our shop right now. Now, it could have been nicer   if we could somehow have a list with all the item  instances that have been created until this point.   But currently, there is not an approach that  will give us a list with five elements where each

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent