DEV Community

M.Ark
M.Ark

Posted on

Classes in python

In the real world, the physical world is made up of material items for example wood, metal,plastic etc. We have to think of these in higher terms to as to understand. For example we think of things as chairs, tables, house etc. Same thing in the programming field,programmers deal with higher level groupings of items-objects.

In programming an object is a grouping of data and operations that can be performed on the data.
Encapsulation occurs when a user interacts with an objects at a higher level , allowing the lower level details to be hidden. This is also known as information hiding or encapsulation. Objects support abstraction by hiding entire groups of functions and variables and exposing only certain functions to a user.

Example

  1. Abstraction simplifies our world. An oven is viewed as having a compartment for food and a knob is turned to heat the food.
  2. People need not be concerned with an oven's internal workings. Ex: People don't reach inside to adjust the flame.
  3. Similarly, an object has operations that a user applies. The object's internal data, and possibly other operations, are hidden from the user.

Multiple variables are frequently closely related and should be treated as one variable with multiple parts. For example, two variables called hours and minutes might be grouped together in a single variable called time. The Class keyword can be used to create a user-defined type of object containing groups of related variables and functions.

syntax

class ClassName:
# Statement-1
# Statement-2
# ...
# Statement-N

A class defines a new type that can group data and functions to form an object. The object maintains a set of attributes that determines the data and behavior of the class.
Here is an example:

class Time:
     def __init__(self):
         self.hours=0
         self.minutes = 0

Enter fullscreen mode Exit fullscreen mode

From the sample code above, a programmer can the use instantiation to define anew Time class variable and access the variable attributes.
An instantiation operation is performed by "calling" the class, using parentheses like a function call as in my_time = Time().

An instantiation operation creates an instance, which is an individual object of the given class. An instantiation operation automatically calls the init method defined in the class definition.

A method is a function defined within a class. The init method, commonly known as a constructor, is responsible for setting up the initial state of the new instance. In the example above, the init method creates two new attributes, hours and minutes, and assigns default values of 0.

The init method has a single parameter, "self", that automatically references the instance being created. To create a new attribute, hours, we write an expression such as self.hours = 0 within the init method.

Example

class Time:
    def __init__(self):
        self.hours = 0
        self.minutes = 0


my_time = Time()
my_time.hours = 7
my_time.minutes = 15

print(f'{my_time.hours} hours', end=' ')
print(f'  {my_time.minutes} minutes')
Enter fullscreen mode Exit fullscreen mode

The output of the code above will be 5 hours and 46 minutes.

In the code sample above, we have used instantiation to create a variable using the Time class.
In the same manner, we can create multiple instances of a class in a program, with each instance having different attribute values.

Example

class Time:
    def __init__(self):
        self.hours = 0
        self.minutes = 0


time1 = Time()  # instance called time1
time1.hours = 3
time1.minutes = 39

time2 = Time()  # a second instance called time2
time2.hours = 4
time2.minutes = 58

print(f'{time1.hours} hours and {time1.minutes} minutes')
print(f'{time2.hours} hours and {time2.minutes} minutes')

Enter fullscreen mode Exit fullscreen mode

The output of the code above is 3hours and 39 minutes
4 hours and 58 minutes.

In the code above we have two instances of the Time class being instantiated. As we said initially, instantiation is done by calling the class.
We have time1 and time2 being the instances of the Time class.

Top comments (0)