Python 3 Classes - Objects

Python 3: From Scratch to Intermediate ADVANCED LEVEL: Python 3 - Classes and Objects
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.59
List Price:  €131.03
You save:  €37.43
£80.45
List Price:  £112.64
You save:  £32.18
CA$136.81
List Price:  CA$191.54
You save:  CA$54.73
A$153.85
List Price:  A$215.39
You save:  A$61.54
S$136.10
List Price:  S$190.55
You save:  S$54.44
HK$783.19
List Price:  HK$1,096.50
You save:  HK$313.30
CHF 91.41
List Price:  CHF 127.98
You save:  CHF 36.57
NOK kr1,095.33
List Price:  NOK kr1,533.51
You save:  NOK kr438.17
DKK kr697.93
List Price:  DKK kr977.13
You save:  DKK kr279.20
NZ$168.49
List Price:  NZ$235.89
You save:  NZ$67.40
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,960.96
List Price:  ৳15,345.79
You save:  ৳4,384.82
₹8,331.14
List Price:  ₹11,663.93
You save:  ₹3,332.78
RM477.70
List Price:  RM668.80
You save:  RM191.10
₦126,037.39
List Price:  ₦176,457.39
You save:  ₦50,420
₨27,728.08
List Price:  ₨38,820.43
You save:  ₨11,092.34
฿3,701.79
List Price:  ฿5,182.66
You save:  ฿1,480.86
₺3,251.67
List Price:  ₺4,552.47
You save:  ₺1,300.80
B$513.17
List Price:  B$718.47
You save:  B$205.29
R1,913.51
List Price:  R2,678.99
You save:  R765.48
Лв183.04
List Price:  Лв256.26
You save:  Лв73.22
₩137,500.16
List Price:  ₩192,505.72
You save:  ₩55,005.56
₪376.08
List Price:  ₪526.53
You save:  ₪150.44
₱5,758.67
List Price:  ₱8,062.37
You save:  ₱2,303.69
¥15,492.10
List Price:  ¥21,689.56
You save:  ¥6,197.46
MX$1,697.24
List Price:  MX$2,376.20
You save:  MX$678.96
QR362.79
List Price:  QR507.92
You save:  QR145.13
P1,387.12
List Price:  P1,942.02
You save:  P554.90
KSh13,448.65
List Price:  KSh18,828.65
You save:  KSh5,380
E£4,787.02
List Price:  E£6,702.02
You save:  E£1,915
ብር5,664.26
List Price:  ብር7,930.19
You save:  ብር2,265.93
Kz83,485.81
List Price:  Kz116,883.48
You save:  Kz33,397.66
CLP$95,360.46
List Price:  CLP$133,508.46
You save:  CLP$38,148
CN¥724.53
List Price:  CN¥1,014.38
You save:  CN¥289.84
RD$5,902.47
List Price:  RD$8,263.69
You save:  RD$2,361.22
DA13,439.62
List Price:  DA18,816
You save:  DA5,376.38
FJ$229.29
List Price:  FJ$321.01
You save:  FJ$91.72
Q776.47
List Price:  Q1,087.09
You save:  Q310.61
GY$20,894.43
List Price:  GY$29,253.04
You save:  GY$8,358.61
ISK kr14,065.59
List Price:  ISK kr19,692.39
You save:  ISK kr5,626.80
DH1,009.26
List Price:  DH1,413.01
You save:  DH403.74
L1,779.82
List Price:  L2,491.82
You save:  L712
ден5,758.50
List Price:  ден8,062.13
You save:  ден2,303.63
MOP$806.12
List Price:  MOP$1,128.60
You save:  MOP$322.48
N$1,913.23
List Price:  N$2,678.60
You save:  N$765.36
C$3,661.78
List Price:  C$5,126.64
You save:  C$1,464.86
रु13,321.12
List Price:  रु18,650.11
You save:  रु5,328.98
S/368.06
List Price:  S/515.29
You save:  S/147.23
K378.12
List Price:  K529.39
You save:  K151.26
SAR375.03
List Price:  SAR525.05
You save:  SAR150.02
ZK2,574.10
List Price:  ZK3,603.84
You save:  ZK1,029.74
L465.65
List Price:  L651.93
You save:  L186.28
Kč2,362.20
List Price:  Kč3,307.17
You save:  Kč944.97
Ft36,810.41
List Price:  Ft51,536.04
You save:  Ft14,725.63
SEK kr1,086.49
List Price:  SEK kr1,521.13
You save:  SEK kr434.64
ARS$87,266.68
List Price:  ARS$122,176.84
You save:  ARS$34,910.16
Bs687.45
List Price:  Bs962.46
You save:  Bs275.01
COP$390,268.69
List Price:  COP$546,391.78
You save:  COP$156,123.08
₡50,117.34
List Price:  ₡70,166.29
You save:  ₡20,048.94
L2,455.43
List Price:  L3,437.70
You save:  L982.27
₲738,156.27
List Price:  ₲1,033,448.32
You save:  ₲295,292.04
$U3,836.09
List Price:  $U5,370.68
You save:  $U1,534.59
zł404.46
List Price:  zł566.27
You save:  zł161.80
Already have an account? Log In

Transcript

Apart from what we've seen until now, Python also has an object oriented approach. Up to this point, we have seen one way of programming in Python using functions. Object Oriented Programming is based on classes, methods and objects. We will analyze what each of these terms means in this lecture and the next one. In short, a class is a data type containing its own variables, attributes and functions, which by the way, in object oriented programming are called methods. A standard definition of a class will tell you that the class is like a blueprint for creating objects.

An object may be regarded as an instance of a defined class, and the attribute values for a particular object define the state of that object. Another term that is very much used when discussing classes is inheritance. This means that the new class may inherit all the names and functionality from an existing class, we will talk more about inheritance in the next lecture. Now let's take this one step at a time. First, we should see how to define a class, I will create a new file on my D drive and write the code inside this file. I will name this my class dot p y.

Okay, so this is the file. In order to properly define a class, you will always use the class keyword and then type in the class name. Now, be careful here because the convention is to use camel case for class names. This means each word in the name of the class will be capitalized. And also no spaces are allowed, actually, except the camel case rule. All the rules regarding variable and function names apply to class names as well.

So let's name our class simply my router after the name of the class in between parentheses, you should type in the word object, all lowercase. This is the new style of defining classes starting with Python. On version two dot two, the thing you should keep in mind here is that if a class doesn't inherit from another class, then you should always type in the word object inside parentheses when defining a class. This is a default setting, and it means that this class inherits from a default class named object. I know this may seem confusing, so we won't get into any more details on this topic. Just don't forget to add that word object.

So we have class my router object. Then, as for any other block of code we've seen so far, we'll type in a colon. On the next row using one level of indentation Of course, we shall input the content of the class. As with functions on the first row after the class definition. You can type in a documentation string or doc string in between quotes to provide a hint about that classes functionality. So let's enter some text in between double quotes.

Okay, so this is a class that describes the correct turistic of router following the optional duck string, the first thing you define inside the class is the special init method, also called a class constructor. The word in it will be preceded and followed by double underscore. This is the way Python identifies a special method. So let me write this, okay. So the role of it is to initialize some variables and the method is called whenever you create a new instance of the class in which it resides. Actually, it is the first code that is executed whenever you create a new instance of the class.

Any special method or regular method within a class is defined using the def keyword, as you do with regular functions. The difference here is that each time you define a method inside the class, the first parameter inside the parentheses is self. You have to remember to always input this word as the first parameter of every class method. Self is no more than just a reference to the current instance of the class. Now after typing in self, you define any other parameters that you want to be defined and initialized whenever you create a new instance of the class in which it resides. In our case, we want to define some parameters that characterize our router or device.

So I have added the router name, model, serial number, and iOS. Now let's define the object or instance attributes we need to describe the router according to the parameters of the init method. Remember, that self is used to point out that we are referring to the current instance of the class. The next lines of code will be again indented one level to the right under the definition of the init method. Okay, so that's how you define object attributes. Now let's also define a new method inside this class.

That will do nothing more than just print out the attributes and concatenate the model of the router. With the manufacturing date, the definition of this new method will sit at the same level of indentation as the definition of the init method. So let me write this, notice that self is again inserted as the first parameter, you should do this for every method you define inside the class. Next, inside the method, again, indenting, our code one level to the right, we enter the print functions we need. Okay, so these are the print functions that will print out the characteristics of this router when calling this method. Now let's see what's the deal with the objects we talked so much about.

An object is actually an instance of the class. You can create as many objects as you want or need by simply calling the class name and entering the arguments required by the init method in between parentheses, all of them except self, which is automatically passed by Python. First of all, let's copy and paste the code inside the file into the Python interpreter. And let's continue Our analysis there. Okay, so here's the Python interpreter. I'm going to paste the code.

Let's hit Enter. And now let's create our first object. Let's call it router one equals the name of the class, my router, and in between parentheses, we are specifying the arguments for the init method inside the class. This is somewhat similar with calling a function by passing arguments to that function. So let me add some values for these parameters. Okay, now let's check router one.

Indeed, we see that we have created an object and Python confirms it. Now what can you do with this object? First, you can access each of its attributes. Let's see how router one dot model 2600 That is correct. router one dot iOS 12 dot for router one dot serial number. Okay, and finally your alter one dot router name or one which is again correct Cool, what else, we can also access the method we defined inside the class.

This one right here, the method borrows the attributes defined in the init method. So these ones right here, and uses them within its own block of code. Also notice that we should specify a value for the manufacturing date parameter when calling this method. Again, self is invoked by Python in the background, so you shouldn't worry about it. To use the object for calling the method will do the following router, one dot, the name of the method, print router, and now let's pass in the date. Let's say 2018 1010.

Okay, let's hit Enter. And now you can see that the method inside our class has been called using the arguments we passed when creating the object and also this manufacturing date right here, which has been concatenated with the model of this router according to this Definition right here. Now let's create another object and do the same operations just to prove we can create multiple objects. So this is router to a new object. Now let's check a few attributes router to that iOS. And this is true that true now, router to that router name.

Indeed, we have our two and also Let's call the method inside the class router two dot print router. And let's pass another date as an argument. 2019 0101. Okay, great. So you can create as many objects as you want. You can also change an attribute of an object like this.

So let's say router two dot iOS, which is currently equal to 12 dot two. Now let's assign a new value for this attribute router two dot iOS equals 12 dot three. Now let's check the attribute again. Okay, it has been updated. Python also provides some functions to play around with object attribute Let's see them First we have the get addr function for getting the value of an attribute. Let's use it on the router to object so get a TDR of router two and the attribute iOS and indeed we get that three.

We can also set a new value foreign attribute using the set addr function. So set addr for router two, comma, the attribute we want to set a new value for iOS and the new value 12 dot one. Okay, now if we use get at tr again, the value has been indeed updated. Another function you can use is has a tr to check whether an object attribute exists or not. So let's try has a tr for object router to sorry I misspelled the name of the function so has a tr of router two and the attributes iOS, this should return true because iOS is indeed an attribute of this object. Okay, let's also check for iOS two, and this is false.

Finally, you can delete an attribute using the del addr function. So let's try del addr of router to karma iOS. Now let's check if the object still has this attribute. So has a TTR of router to an iOS, and this time we get false returns. So indeed, it doesn't have the attribute anymore. Let's also see what error does Python return when trying to get a non existing attribute using the get addr function.

So let's try again get a TTR of router two and iOS. So as expected, we got an attribute error my router object has no attribute iOS, we can also verify if an object is an instance of a particular class. This is used For especially when you have multiple classes and objects created, and you want to keep track of them. Let's see this in action for router two. So the name of the function is is instance, router, two, comma, and now the name of the class my router, and the result is true, we have the confirmation that the object named router two is indeed an instance of the my router class. In short, this is how object oriented programming or simply op works.

We want to use classes and object oriented programming to build the applications in this course, although we could do it, it's really a matter of choice and to be honest, I like the classical way of programming more. Next we'll have a look at class inheritance.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.