Python Programming

Python Classes and Objects With Examples

 

Creating Python classes

Related variables and methods are grouped together in python classes. The easiest  form of python classes definition looks like this:

class ClassName:

<statement-1>

<statement-N>

Python Classes are defined by using the class keyword, followed by the ClassName and a colon. Python class definitions must be executed before they have any effect. In practice, the statements inside a python class definition will usually be function definitions, but few other statements are allowed. (We’ll discuss this later). Because these functions are indented under python classes, they are called methods. Methods are a special kind of function that is defined within python classes.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Example: Program to Illustrate Python classes and Object Creation:

class Mobile:
def __init__(self):
print("This message is from Constructor Method")
def receive_message(self):
print("Receive message using Mobile")
def send_message(self):
292 Introduction to Python Programming
print("Send message using Mobile")
def main():
nokia = Mobile()
nokia.receive_message()
nokia.send_message()
if __name__ == "__main__":
main()

Output
This message is from Constructor Method
Receive message using Mobile
Send message using Mobile

Let’s define python class called Mobile that has two methods associated with it one is receive_message() and another is send_message(). The first parameter in each of these methods is the word self. When self is used, it is just a variable name to which the object that was created based on python classes is assigned. In the method definition, self doesn’t need to be the only parameter and it can have multiple parameters. Creating the Mobile class provided us with a blueprint for an object. Just because you have defined a python class doesn’t mean you have created any Mobile objects. Often, the first argument of a function is called self. This is nothing more than an assembly: the name self has absolutely no special meaning to Python. However, by not following this convention, your code may be less readable to other Python programmers.



Creating Objects in Python

Object refers to a particular instance of a python class where the object contains variables and methods defined in the python classes. Python class objects accept two kinds of operations: attribute references and instantiation. The term attribute refers to any name (variables or methods) following a dot. This is a syntactic construct. The act of making an object from a python class is called instantiation. The names in the python classes are referenced by objects and are called attribute references. There are two kinds of attribute references, data attributes, and method attributes. Variables defined within the methods are called instance variables and are used to store data values. New

instance variables are associated with each of the objects that are created for a python classes. These instance variables are also called data attributes. Method attributes are methods inside a python classes and are referenced by objects of a class. Attribute references use the standard dot notation syntax as supported in Python.

The syntax to access data attribute is,

object_name.data_attribute_name

The syntax to assign value to data attribute is,

object_name.date_attribute_name = value

where value can be of integer, float, string types, or another object itself.

The syntax to call method attribute is,

object_name.method_attribute_name()

Valid attribute names are all the names that were inside the python classes when the objects for the python classes was created. The connection between the attributes with the object is indicated by a “dot” (“.”) written between them with object_name on left and attribute_name on right. For example, in the expression z.real = 10, real is an data attribute of the object z and is assigned a value of 10. In the expression cow.domesticated(), the domesticated() is a method attribute of the cow object.

The syntax for Python classes instantiation is,

object_name = ClassName(argument_1, argument_2, ….., argument_n)

Optional

Python classes instantiation uses function notation, wherein the class name is followed by

parentheses () as if it were a function, nokia = Mobile(). The above expression creates a new object for the python classes ClassName and assigns this object to the variable object_name.

You can specify any number of arguments during instantiation of the python classes object. An object nokia  for the class Mobile is created. The nokia object calls the methods receive_message() and send_ message() using the dot operator. Calling nokia.receive_message() and nokia.send_message() means that these methods are to be used with a nokia instance of the class Mobile.

You may have noticed that both of these method definitions have self as the first parameter. This self variable can also be used inside the method bodies, but you do not appear to pass this as an argument in the method called using the object. This is because whenever you call a method using an object, the object itself is automatically passed in as the first parameter to the self parameter variable. The remaining parameter variables must be supplied as arguments in the calling method. The object nokia calls two methods in the main() function of the program, causing those methods to run. Python raises an exception when a method that requires an argument is called without any, even if the argument is not actually used.


Python classes with Multiple Objects

Multiple objects for a python classes can be created while attaching a unique copy of data attributes and methods of the python classes to each of these objects.

Example: Program to Illustrate the Creation of Multiple Objects for Python classes:

class Birds:
def __init__(self, bird_name):
self.bird_name = bird_name
def flying_birds(self):
print(f"{self.bird_name} flies above clouds")
def non_flying_birds(self):
print(f"{self.bird_name} is the national bird of Australia")
def main():
vulture = Birds("Griffon Vulture")
crane = Birds("Common Crane")
emu = Birds("Emu")
vulture.flying_birds()
crane.flying_birds()
emu.non_flying_birds()
if __name__ == "__main__":
main()

Output
Griffon Vulture flies above clouds
Common Crane flies above clouds
Emu is the national bird of Australia

Here, three objects, vulture, crane, and emu, are created for the Birds class. All of these objects belong to the same python classes, so they have the same data attribute but different values for each of those data attributes. Objects can also have their own methods to operate on their data attributes. A method is always begot relative to some object of its python classes. During object instantiation, each object receives a unique copy of data attribute and method is bundled together. This  guarantees that correct data attributes and methods are used that are specific to a particular object. The self variable is initialized with the particular object of the python classes that is created during instantiation  and the parameters of __init__() constructor is initialized with the arguments passed on to that python classes object . Now we have three objects whose data attributes have different values. In this case, vulture.flying_birds() will output “Griffon Vulture flies above clouds,” crane.flyingbirds() will output “Common Crane flies above cloud,” and emu.non_flying_birds() will output “Emu is the national bird of Australia” – . Notice the use of bird_name in and. Even though they have the same name, they are unique. The bird_name in __init__() method definition header is used as a parameter while the bird_name referenced by self within the method is an instance variable.


Example: Write Python Program to Simulate a Bank Account with Support for depositMoney, withdrawMoney and showBalance Operations:

class BankAccount:
def __init__(self, name):
self.user_name = name
self.balance = 0.0
def show_balance(self):
print(f"{self.user_name} has a balance of {self.balance} dollars")
299 Object-Oriented Programming
def withdraw_money(self, amount):
if amount > self.balance:
print("You don't have sufficient funds in your account")
else:
self.balance -= amount
print(f"{self.user_name} has withdrawn an amount of {self.balance} dollars")
def deposit_money(self, amount):
self.balance += amount
print(f"{self.user_name} has deposited an amount of {self.balance} dollars")
def main():
savings_account = BankAccount("Olivia")
savings_account.deposit_money(1000)
savings_account.show_balance()
savings_account.withdraw_money(500)
savings_account.show_balance()
if __name__ == "__main__":
main()

Output
Olivia has deposited an amount of 1000.0 dollars
Olivia has a balance of 1000.0 dollars
Olivia has withdrawn an amount of 500.0 dollars
Olivia has a balance of 500.0 dollars

In the __init__() method, two data attributes, user_name and balance, are added . Also, show_balance(), withdraw_money(), and deposit_money() methods are added to the python classes. The data attribute user_name is initialized with the value of name parameter while balance data attribute is initialized to zero. This value of data attribute balance is changed in methods. The method show_balance() displays user name and the balance user has in his account. In the withdraw_money() method, the user specified amount is compared with the existing balance. If the withdrawal amount is more than the existing balance, then a message is displayed saying, “You don’t have sufficient funds in your account,” or else the amount is subtracted from the balance. In the deposit_money() method, the user specified amount is added to the existing balance – . Using the object savings_account, various methods are referenced . Methods withdraw_money() and deposit_money() have amount as the parameter. This amount is either added or subtracted to the balance data attribute. Inside the methods, the data attribute balance is referenced through self. This shows that not only the data attributes can be accessed within the methods of the same python classes, but also the values of data attributes can be manipulated.

Example: Define a Python class Called Cart that Contains Data Attributes Apples and Oranges. Write Methods that Return Appropriate Messages If the Number of Apples is Greater than 5 or When the Number of Oranges are Greater than 10.

class Cart:
def __init__(self, apples, oranges):
self.apples = apples
self.oranges = oranges
def apple_quantity_check(self):
if self.apples > 5:
return 'Sufficient Quantity'
else:
return 'Insufficient Quantity'
def orange_quantity_check(self):
if self.oranges > 10:
return 'Sufficient Quantity'
else:
return 'Insufficient Quantity'
def main():
fruits = Cart(3, 11)
returned_apple_message = fruits.apple_quantity_check()
returned_orange_message = fruits.orange_quantity_check()
print(f"Apple is in {returned_apple_message}")
print(f"Orange is in {returned_orange_message}")
if __name__ == "__main__":
main()

Output
Apple is in Insufficient Quantity
Orange is in Sufficient Quantity

The instance variables, apples, and oranges, are added to the Cart Class. The apple_ quantity_check()  and orange_quantity_check() _– methods check for the number of apples and oranges and return a string message. Each of these methods – are referenced by fruits object. In the expressions returned_apple_message = fruits.apple_ quantity_ check() and returned_orange_message = fruits.orange_quantity_check(), the left-hand side of the assignment should have a matching number of variables to store the values returned by the return statement from the class method. It is imperative to recognize that apples and oranges parameter variables of __init__() method are independent of apples and oranges of instance variables, as they exist in a different scope.


Example: Program to Demonstrate the Use of Default Parameters in Methods

class Dog:
def __init__(self, breed="German Shepherd", color="Tan Black"):
self.breed = breed
self.color = color
def dog_breed(self):
print(f"Dog Breed is {self.breed}")
def dog_color(self):
print(f"Dog Color is {self.color}")
def main():
babloo = Dog()
babloo.dog_breed()
babloo.dog_color()
if __name__ == "__main__":
main()

Output
Dog Breed is German Shepherd
Dog Color is Tan Black

In the Dog class, the parameters of the __init__() method have default values. If no arguments are specified in Dog() _ while creating an instance of the class, then the default values set for the __init__() method parameters gets assigned to instance variables. If you specify arguments in Dog(), then the default values assigned to __init__() method parameters will be overwritten with the latest values.

Using Objects as Arguments

An object can be passed to a calling function as an argument.

Example: Program to Demonstrate Passing of an Object as an Argument to a Function Call

class Track:
def __init__(self, song, artist):
self.song = song
self.artist = artist
def print_track_info(vocalist):
print(f"Song is '{vocalist.song}'")
print(f"Artist is '{vocalist.artist}'")
singer = Track("The First Time Ever I Saw Your Face", "Roberta Flack")
print_track_info(singer)

Output
Song is "The First Time Ever I Saw Your Face"
Artist is "Roberta Flack"

In the python classes Track, the __init__() method is added with the song and artist data attributes. The print_track_info() function receives an object as parameter. The object singer of Track class is passed as an argument to print_track_info() function. (Note: It is a function defined outside the python classes and not a method.) Since you are passing an object of a python class as an argument, you have access to all the data attributes attached to that object.



Example: Given Three Points (x1, y1), (x2, y2) and (x3, y3), Write a Python Program to Check If they are Collinear

class Collinear:
def __init__(self, x, y):
self.x_coord = x
self.y_coord = y
def check_for_collinear(self, point_2_obj, point_3_obj):
if (point_3_obj.y_coord - point_2_obj.y_coord)*(point_2_obj.x_coord -
self.x_coord) == (point_2_obj.y_coord - self.y_coord)*(point_3_obj.x_coord -
point_2_obj.x_coord):
print("Points are Collinear")
else:
print("Points are not Collinear")
def main():
point_1 = Collinear(1, 5)
point_2 = Collinear(2, 5)
point_3 = Collinear(4, 6)
point_1.check_for_collinear(point_2, point_3)
if __name__ == "__main__":
main()

Output
Points are Collinear

Three objects, point_1, point_2 and point_3, are created for Collinear python classes –. Each of these objects have their own unique data attributes with their associated values. The __init__() method has two data attributes x_coord and y_coord. The method check_for_ collinear() checks whether the coordinates are collinear or not. The method check_for_ collinear() takes three objects as its parameters. The method check_for_ collinear() is invoked using point_1 object while point_2 and point_3 objects are passed as arguments. On invoking the check_for_collinear()  method, the point_1 object is assigned to self parameter, point_2 object in the argument is assigned to point_2_obj of the parameter, and point_3 object in the argument is assigned to point_3_obj of the parameter in the check_for_ collinear() method header. When each of these objects are created, each object gets its own unique copy of data attributes defined by that python classes. The x_coord and y_coord data attributes for point_1 object have values of 1 and 5, the x_coord and y_coord data attributes for point_2 object have values of 2 and 5, and the x_coord and y_coord data attributes for point_3 object have values of 4 and 6. Three or more segments A, B, C,…, are said to collide if they lie on a single direct line. If the line points AB and BC have the same slope, then A, B, C are necessarily collinear. collided with for three points A(a, b), B(m, n), and C(x, y)

are checked using the formula (n – b) (x – m) = (y – n) (m – a).

Objects as Return Values

It is important to note that everything in Python is an object, including classes. In Python, “everything is an object” (that is, all values are objects) because Python does not include any primitive, unboxed values. Anything that can be used as a value (int, str, float, functions, modules, etc.) is implemented as an object. The id() function is used to find the identity of the location of the object in memory. The syntax for id() function is,

id(object)

This function returns the “identity” of an object. This is an integer (or long integer), which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. You can check whether an object is an instance of a given python class or not by using the

isinstance() function. The syntax for isinstance() function is,

Object diagram for Collinear class with different objects.

isinstance (object, classinfo)

where the object is an object instance and classinfo can be a python class, or a tuple containing classes, or other tuples. The isinstance() function returns a Boolean stating whether the object is an instance or subclass of another object.


Example: Given the Coordinates (x, y) of a Center of a Circle and Its Radius, Write Python Program to Determine Whether the Point Lies Inside the Circle, On the Circle or Outside the Circle

class Circle:
def __init__(self, radius=0, circle_x=0, circle_y=0, point_x=0, point_y=0):
self.radius = radius
self.circle_x_coord = circle_x
self.circle_y_coord = circle_y
self.point_x_coord = point_x
self.point_y_coord = point_y
self.status = ""
def check_point_status(self):
if (self.point_x_coord - self.circle_x_coord) ** 2 + (self.point_y_coord - self.
circle_y_coord) ** 2 < self.radius ** 2:
self.status = f"Point with coordinates {(self.point_x_coord, self.point_y_
coord)} is inside the Circle"
elif (self.point_x_coord - self.circle_x_coord) ** 2 + (self.point_y_coord - self.
circle_y_coord) ** 2 > self.radius ** 2:
self.status = f"Point with coordinates {(self.point_x_coord, self.point_y_
coord)} is outside the Circle"
else:
self.status = f"Point with coordinates {(self.point_x_coord, self.point_y_
coord)} is on the Circle"
return self
def main():
point = Circle(10, 2, 3, 9, 9)
returned_object = point.check_point_status()
print(returned_object.status)
print(f"Is point an instance of Circle Class? {isinstance(point, Circle)}")
print(f"Is returned_object an instance of Circle Class? {isinstance(returned_
object, Circle)}")
305 Object-Oriented Programming
print(f"Identity of the location of a point object is {id(point)}")
print(f"Identity of the location of the returned_object object is {id(returned_object)}")
if __name__ == "__main__":
main()
Output
Point with coordinates (9, 9) is inside the Circle
Is point an instance of Circle Class? True
Is returned_object an instance of Circle Class? True
Identity of the location of a point object is 2351304741216
Identity of the location of the returned_object object is 2351304741216

If you have a circle with the center as (center_x, center_y) and radius as radius, then you can test if a given point with coordinates (x, y) is inside or outside, or on the circle using the formula (x – center_x) ^ 2 + (y – center_y) ^ 2 < radius ^ 2. Please note, the points that satisfy this equation with < operator replaced by == operator are considered to be on the circle, and the points that satisfy this equation with < operator replaced by > operator are considered to be outside the circle.

The point object is used to invoke check_point_status() method. This check_point_status() method returns the self object itself. The returned_object variable is used to store the returned object. Both point and returned_object are used to store an instance of the same python classes – and they point to the same location in memory, which is why their values are the same.


Example: Write the Pythonic Program to Compute the End Time of an Opera, While Start Time and Duration are Given

class Time:
def __init__(self, hours, minutes, seconds):
self.hours = hours
self.minutes = minutes
self.seconds = seconds
def add_time(self, duration):
opera_hours = self.hours + duration.hours
opera_minutes = self.minutes + duration.minutes
opera_seconds = self.seconds + duration.seconds
while opera_seconds >= 60:
opera_seconds = opera_seconds – 60
306 Introduction to Python Programming
opera_minutes = opera_minutes + 1
while opera_minutes >= 60:
opera_minutes = opera_minutes – 60
opera_hours = opera_hours + 1
print(f"Opera ends at {opera_hours}:{opera_minutes}:{opera_seconds}")
def main():
opera_start = Time(10, 30, 30)
opera_duration = Time(2, 45, 50)
opera_start.add_time(opera_duration)
if __name__ == "__main__":
main()

Output
Opera ends at 13:16:20

In the Time class, three data attributes, hours, minutes, and seconds, are added. The add_time() method is invoked using opera_start object, while the opera_duration object is passed as an argument – . In the add_time() method, self parameter is assigned with the opera_start object, and the duration parameter is assigned with opera_duration object. The hours, minutes, and seconds data attributes attached to both of these objects are added and assigned to opera_hours, opera_minutes, and opera_seconds variables. While the total duration of opera_seconds variable is greater than sixty, the decrement of opera_ seconds is done by a value of sixty and increment opera_minutes by one. While the total duration of the opera_ minutes variable is greater than sixty, then decrement opera_ minutes by a value of sixty and increment the opera_hours by one. Finally, print the opera end time.

Python classes Attributes versus Data Attributes

Generally speaking, Data attributes are instance variables that are unique to each object of python classes, and Python classes attributes are class variables that is shared by all objects of a class.


Example: Program to Illustrate Python classes Variables and Instance Variables

class Dog:
kind = 'canine'
def __init__(self, name):
self.dog_name = name
d = Dog('Fido')
e = Dog('Buddy')
print(f"Value for Shared Variable or Class Variable 'kind' is '{d.kind}'")
307 Object-Oriented Programming
print(f"Value for Shared Variable or Class Variable 'kind' is '{e.kind}'")
print(f"Value for Unique Variable or Instance Variable 'dog_name' is '{d.
dog_name}'")
print(f"Value for Unique Variable or Instance Variable 'dog_name' is '{e.dog_name}'")

Output
Value for Shared Variable or Class Variable 'kind' is 'canine'
Value for Shared Variable or Class Variable 'kind' is 'canine'
Value for Unique Variable or Instance Variable 'dog_name' is 'Fido'
Value for Unique Variable or Instance Variable 'dog_name' is 'Buddy'

Here, the variable kind is a class variable and is shared by all the objects. The instance variable dog_name is unique to each of the objects created. You can access both python class variables and instance variables using dot notation. The objects d and e of the class Dog share the same python classes variable kind. However, for instance, variable dog_name, the self parameter is replaced with the object created. The instance variable dog_name is unique to each of the d and e objects, which were created resulting in the printing of different values associated with each of these objects.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button