Python Classes and Objects With Examples

Python Classes

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 classes definitions must be executed before they have any effect. In practice, the statements inside a python classes definition will usually be function definitions, but few other statements are allowed. (We’ll discuss this later). Because these functions are indented under a python classes, they are called methods. Methods are a special kind of function that is defined within python classes.



Example: Program to Illustrate Python classes and Object Creation:

Let’s define a python classes 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 a 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 classes doesn’t mean you have created any Mobile objects. Often, the first argument of a function is called self. This is nothing more than a 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 classes where the object contains variables and methods defined in the python classes. Python classes 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 classes is called instantiation. The names in a 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:

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:

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.

The instance variables, apples and oranges, are added to the Cart Class. The apple_ quantity_check()  and orange_quantity_check() _– methods check for the quantity 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

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

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 classes 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

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 argument is assigned to point_2_obj of the parameter, and point_3 object in 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 be 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 classes 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 classes, 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

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

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 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 a 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

Here, the variable kind is a class variable and is shared by all the objects. The instancevariable dog_name is unique to each of the objects created. You can access both python classes 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.

Related Article:

Python Recursion Factorial And Fibonacci Sequence In Python

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: