2. • Python is an object oriented programming.
• Almost everything in Python is an object, with it’s properties and methods.
• A class is like an object constructor or a ‘blue print’ for creating objects.
In order to create
eiffiel tower we will
need a blueprint of
the tower
3. • A Python class is like an outline for creating a new object. An object is anything that
you wish to manipulate or change while working through the code.
• In short, a Python class is for defining a particular type of object. Because Python
objects can have both function and data elements, Python classes define what
methods can be used to change the state of an object. They also indicate what
attributes the object can have.
4. CREATING CLASS
Example : Create a class name MyClass with a
property named x.
◦ Input:
◦ Output:
Now we can use class named MyClass to create an
object.
◦ Input:
◦ Output:
To create a class use the keyword class:
5. o To understand the meaning of classes we have to understand the built-in __init__()
function.
o The __init__ function is called every time an object is created from a class.
o Use the __init__() function to assign values to object properties, or other operations
that are necessary to do when the object is being created:
THE __init__() FUNCTION
6. Example: Create a class named Person, use the __init__() function to
assign values for name and age:
Input: Output:
7. SELF VARIABLE
• The self variable is used to represent the instance of the class
which is often used in object-oriented programming.
• It works as a reference to the object.
• Python uses the self parameter to refer to instance attributes and
methods of the class.
8. What is self constructor?
◦ The self variable in Python can also be used to access a variable field within the class definition.
◦ Example:
class Student:
In the example above, self refers to the variable name of the class Student. The variable name is local to the method. While the
method is running, the variable name exists within the class.