The classes are used to represent real-world situations. Once written, we spend most of our time working with instances created from that class. One of the major tasks we usually do is to modify the attributes associated with a particular instance. It is possible to modify the attributes of an instance directly or write methods that update attributes in specific ways.
Let's create a class representing a Motorbike as shown below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose ")
This class stores information about the bike we are working with and defines a method bike_description which provides the bike details. Thus we have a class representing a real world situation now we'll create instances of this class to access the method defined in the class. See the code below -
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
We created an instance from the Motorbike class and store it in the variable my_bike. Then we call bike_description() to show what kind of bike came to the garage. The output of this program is shown below -
Working with classes and instances
Now let us add an attribute charges to our Motorbike class which indicates the bike's servicing charges. Also we'll create a new method get_amount to get the servicing charges. See the program below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe minimum servicing charges for the bike is " + self.charges +" actual billing amount may vary")
This time when Python calls the __init__() method to create a new instance it creates a new attribute
called charges and sets its initial value to 700
Now using our instance we get access this get_amount() as shown below -
my_bike.get_amount()
The output of the program is shown below -
Modifying the attributes of a class
In the above program we have fixed the value of charges attribute to 700 which is the minimum billable amount for a bike's servicing but in reality the billing amount may increase depending on the additional repairing and other jobs done during servicing. Thus we might have to change the value of charges attribute on case by case basis.
An attribute’s value can be changed in three ways: you can change the value directly through an instance, set the value through a method, or increment the value (add a certain amount to it) through a method. Let’s look at each of these approaches.
1. Direct modification through an instance
This is the easiest way to modify an attributes value. Suppose our final billing amount for the bike servicing is 900, then we can change the charges attribute directly as shown -
my_bike.charges =900
The complete program is shown below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe servicing charges for the bike is " + str(self.charges))
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
my_bike.charges =900
my_bike.get_amount()
The output of this program is shown below -
As you may have noticed, the servicing charges now changed from 700 to 900.
2. Modification through a method
Instead of accessing the attribute directly, you pass the new value to a method that handles the updating internally. One way is to change our get_amount() as shown below -
def get_amount(self,charges):
self.charges=charges
print("\nThe servicing charges for the bike is " + str(self.charges))
The other option is to write a new method which would change the attribute value as shown below -
def billing_amount(self,final_amount):
self.final_amount=final_amount
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
In this approach both the default and new amounts are available as shown in the program below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe minimum servicing charges for the bike is " + str(self.charges))
def billing_amount(self,final_amount):
self.final_amount=final_amount
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
my_bike.get_amount()
my_bike.billing_amount(900)
The output of this program is shown below -
Here we have the minimum as well as final charges available for printing.
3. Incrementing the attribute's value through a method
Suppose we charge for parking the vehicle in service station if the vehicle is out for delivery and the customer doesn't pick the vehicle within 48 hours. In this case our final amount will include parking charges as well as servicing charges. We include another method as shown below -
def new_amount(self,final_amount):
self.final_amount+=self.parking
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
So if the bike is parked in service station for more than 48 hrs after servicing then we will call this method with the instance of Motorbike class. See the complete program below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
self.parking=100
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe minimum servicing charges for the bike is " + str(self.charges))
def billing_amount(self,final_amount):
self.final_amount=final_amount
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
def new_amount(self,final_amount):
self.final_amount+=self.parking
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
my_bike.get_amount()
my_bike.new_amount(900)
The out for the program is shown below -
Here we can see all the three charges, the minimum , actual servicing charges and the net payable amount including parking charges.
With this our discussion comes to an end. Make some class on your own and try to play around with the code to modify the attribute values. In the next post we shall discuss about inheritance. Till then keep practicing and learning Python as Python is easy to learn!
Let's create a class representing a Motorbike as shown below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose ")
This class stores information about the bike we are working with and defines a method bike_description which provides the bike details. Thus we have a class representing a real world situation now we'll create instances of this class to access the method defined in the class. See the code below -
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
We created an instance from the Motorbike class and store it in the variable my_bike. Then we call bike_description() to show what kind of bike came to the garage. The output of this program is shown below -
Working with classes and instances
Now let us add an attribute charges to our Motorbike class which indicates the bike's servicing charges. Also we'll create a new method get_amount to get the servicing charges. See the program below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe minimum servicing charges for the bike is " + self.charges +" actual billing amount may vary")
This time when Python calls the __init__() method to create a new instance it creates a new attribute
called charges and sets its initial value to 700
Now using our instance we get access this get_amount() as shown below -
my_bike.get_amount()
The output of the program is shown below -
Modifying the attributes of a class
In the above program we have fixed the value of charges attribute to 700 which is the minimum billable amount for a bike's servicing but in reality the billing amount may increase depending on the additional repairing and other jobs done during servicing. Thus we might have to change the value of charges attribute on case by case basis.
An attribute’s value can be changed in three ways: you can change the value directly through an instance, set the value through a method, or increment the value (add a certain amount to it) through a method. Let’s look at each of these approaches.
1. Direct modification through an instance
This is the easiest way to modify an attributes value. Suppose our final billing amount for the bike servicing is 900, then we can change the charges attribute directly as shown -
my_bike.charges =900
The complete program is shown below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe servicing charges for the bike is " + str(self.charges))
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
my_bike.charges =900
my_bike.get_amount()
The output of this program is shown below -
As you may have noticed, the servicing charges now changed from 700 to 900.
2. Modification through a method
Instead of accessing the attribute directly, you pass the new value to a method that handles the updating internally. One way is to change our get_amount() as shown below -
def get_amount(self,charges):
self.charges=charges
print("\nThe servicing charges for the bike is " + str(self.charges))
The other option is to write a new method which would change the attribute value as shown below -
def billing_amount(self,final_amount):
self.final_amount=final_amount
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
In this approach both the default and new amounts are available as shown in the program below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe minimum servicing charges for the bike is " + str(self.charges))
def billing_amount(self,final_amount):
self.final_amount=final_amount
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
my_bike.get_amount()
my_bike.billing_amount(900)
The output of this program is shown below -
Here we have the minimum as well as final charges available for printing.
3. Incrementing the attribute's value through a method
Suppose we charge for parking the vehicle in service station if the vehicle is out for delivery and the customer doesn't pick the vehicle within 48 hours. In this case our final amount will include parking charges as well as servicing charges. We include another method as shown below -
def new_amount(self,final_amount):
self.final_amount+=self.parking
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
So if the bike is parked in service station for more than 48 hrs after servicing then we will call this method with the instance of Motorbike class. See the complete program below -
class Motorbike():
def __init__(self,name,color,purpose):
self.name=name
self.color=color
self.purpose=purpose
self.charges=700
self.parking=100
def bike_description(self):
print("\nYou just got " + self.name + " bike of " + self.color + " color in your garage for " + self.purpose )
def get_amount(self):
print("\nThe minimum servicing charges for the bike is " + str(self.charges))
def billing_amount(self,final_amount):
self.final_amount=final_amount
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
def new_amount(self,final_amount):
self.final_amount+=self.parking
print("\nThe final servicing charges for the bike is " + str(self.final_amount))
my_bike = Motorbike('Honda','white','servicing')
my_bike.bike_description()
my_bike.get_amount()
my_bike.new_amount(900)
The out for the program is shown below -
Here we can see all the three charges, the minimum , actual servicing charges and the net payable amount including parking charges.
With this our discussion comes to an end. Make some class on your own and try to play around with the code to modify the attribute values. In the next post we shall discuss about inheritance. Till then keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment