Python classes/objects

Python classes/objects
Python is an OOP (object oriented programming language).

Almost each and every thing in Python is an object, that has its properties and methods.

A Class is like an object constructor, or we can say it a "blueprint" for creating the objects.

Create a Class
To create a class in python we use the keyword class and then write the name of the class we are creating.
For e.g. if we want to create a class named MyClass, with a property named x, then we need to write the following code:

class MyClass:

     x = 5


Create Object
Now, since the class myClass has been created, so the object of that class can be created.
For e.g. if we want to create an object named p1 of the class myClass, and print the value of x, then we have to write the following code for that:

p1 = MyClass()

print(p1.x)


The __init__() Function
The above examples are not much useful in real life applications as the classes and objects are in their simplest form.

Before undderstanding the meaning of classes let us first understand about the built in function __init__()

Each class have a function that is called as __init__(), and this function is always executed when the class is initiated.

For e.g. if we want to use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:
For better understading this let us create a class named Person and we use the __init__() function to assign the values to name and age:

class Person:

     def __init__(self, name, age):

         self.name = name

         self.age = age

p1 = Person("John", 36)

print(p1.name)

print(p1.age)


Object Methods
Methods can also be contained by the objects. The functions that belong to object are the methods in the objects.

Let us create a method in the Person class:
Insert a function that prints a greeting, and execute it on the p1 object:

class Person:

     def __init__(self, name, age):

         self.name = name

         self.age = age

def myfunc(self):

     print("Hello my name is " + self.name)

p1 = Person("John", 36)

p1.myfunc()


The self Parameter
The salf parameter is a parameter that is a reference to current instance of the class and the variables that belong to the class can be accessed with the use of self parameter.

It is not necessary that you have to name this parameter as "self", you can give it any name you want but the condition to be fulfilled is that it shoulld always be the first parameter of any function in the class:
For e.g. we can se the words mysillyobject and abc instead of the self parameter:

class Person:

     def __init__(mysillyobject, name, age):

         mysillyobject.name = name

         mysillyobject.age = age

def myfunc(abc):

     print("Hello my name is " + abc.name)

p1 = Person("John", 36)

p1.myfunc()


Modify Object Properties
The properties of the objects can be modified as follows:
For e.g. we can set the age of p1 to 40:

p1.age = 40


Delete Object Properties
The properties of the objects can be deleted with the use of the "del" keyword.
For e.g. if you want to delete the age property from the p1 object, then you can write the following code:

del p1.age


Delete Objects
The object that has been created can be deleted with the use of "del" keyword.
For e.g. if you want to delete the p1 object, then you can write the following code:

del p1