This content originally appeared on DEV Community and was authored by M.Ark
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
- Abstraction simplifies our world. An oven is viewed as having a compartment for food and a knob is turned to heat the food.
- People need not be concerned with an oven's internal workings. Ex: People don't reach inside to adjust the flame.
- 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
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:
""" A class that represents a time of day """
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'and {my_time.minutes} minutes')
This content originally appeared on DEV Community and was authored by M.Ark
M.Ark | Sciencx (2026-04-10T10:20:14+00:00) Classes in python. Retrieved from https://www.scien.cx/2026/04/10/classes-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.