Python As An Object Oriented Language

Welcome back, in this tutorial we will be discussing use of classes and even objects in python language. We will see how you can create either object or class, object methods, how you ca delete or modify objects properties. Let’s get started;

Python as an high level programming language it has objects that’s why it is referred to as object object oriented language, whereby each object has properties and eve methods.

Let’s now create a class;

class MyClass:
 y = 8

We always create objects from classes, look at this example;

obj = MyClass()
print(obj.y)

The_init_() function

This function is made to understand more about classes and objects. It is very common in each class and it is executed when you are initiating a class. It is also used to assign values to object properties, or may be used for any other operations that are required to be done when creating an object. Let’s look into the following example;

class person:
 def _init_(self, name, country):
 self.name = name
 self.country = country
obj = person("Philloh", "Kenya")
print(obj.name)
print(obj.country)

Object Methods

Objects have methods, they are functions that belongs to the object. Let’s have an example here;

class person:
 def _init_(self, name, country):
 self.name = name
 self.country = country
 def myfunc(self):
  print("Hey i come from " + self.country)
obj = person("Philloh", "Kenya")
obj.myfunc()

How to modify object properties?

You may alter the values of the object or change anything in the properties on objects. Look at this example;

obj.country = "Uganda"

How you can delete object properties?

The same way you can delete or remove any property on any objects using del keyword. Look at this example here;

#you can delete the object as follows
del obj

#you can delete the country property as follows
del obj.country

NB: self parameter as we have applied it mostly refers to the current instance of the class and is used to access variables for the class. Is not must to be saved as self but should the first parameter of any function in the class.

Thanks for reading through our tutorial today, as we continue learning about python hoping to meet you in our next tutorial. You may leave your opinion in the comment section below.