23 Implementation of Encapsulation in class in python programming.

Опубликовано: 03 Апрель 2026
на канале: Free Tutorial TV
4
1

And let me remove the print statements because we don't want them right now.

Now let's say we want to change the value of speed.

So if you want to change the value of speed you can simply take your object for example for the ford dot

your attribute name.

For example speed and then you can directly change it.

For example 300.

And when I run the program it's going to print the news speed which is 300.

Initially we have set the speed to 200 in here.

We can also put let's say the speed string which is very stupid but will it work.

It works.

You can see here and this can break the function in which the speed is used.

So therefore it is very important that we protect our data and only give the access to our data to the

other users.

So encapsulation is particularly important when you want to give it your code to some other people because

they might want to change your code.

So let's see how we can encapsulate our code.

So to encapsulate our code we create functions.

So let's create a first function which is to set the value of speed.

So I'm going to just say set underscore speed and inside the parenthesis it's going to take self as

the first argument which is default.

And second is the value of speed.

So.

So I can say value here.

Right.

And then once we have the value of speed we can just call self.

dot speed is equal to value.

Similarly we can create our get method for the speed.

So right now we have created a set of method to set the speed.

And now we are going to create a get method to get the speed . and in order to get the speed we don't

need to pass any argument. we just need to return the speed which is already set.

So we can just write it down and then self dot speed.

So we have created two functions set and get.

These are also called setter and getter for the attribute which is speed.

In our case.

So let's use these functions instead of for this kind of setting of data.

So first of all I'm going to call this for the object ford dot set speed method out of this object.

And then I'm going to pass the new speed using this set method.

Now in order to get the speed I can write instead of this ford dot speed. I can just write ford dot get

speed . which is going to give me the speed of this ford the object.

**************************************************************************
please like and follow us in Facebook and Instagram.
facebook:   / technologytv  


Instagram:   / heikhamsingh  


Twitter:   / hanandkumarsing