Well Is Python an object oriented programming language? Yes, it is. With the exception of control flow, everything in Python is an object. By Max Fischer, who answers this question in Quora.
Yes, python is an objected oriented programming language. Everything in python is an object. Using python we can create classes and objects. Here in this tutorial, I would be discussing only about classes and objects.
Classes: Class is like a blueprint that helps to create an object. In other words, a class comprises variables, methods, functions. Or you can refer class which contains properties and behavior. For example Consider a class “Ball” now the properties of Ball might be color, diameter, price and the behavior of the ball might be rolling, bouncing.
Objects: Objects are an instance of a class. With the help of objects, we can access the method and function of a class.
Creating a class in Python is trivial, all you have to do is use a keyword called “class” along with a name which is often referred to as the name of the class. Inside the class, we can either declare functions or variables.
class python:
def designer():
print("Guido van Rossum")
def first_appeared():
print("1990; 29 years ago")
In order to access the function inside the class all you have to do call the class with its class name and with the “.” dot operator call its function. We can access the class in two ways: Without creating the object and creating an object
Just use the class name along with the dot operator to access the functions inside the class.
python.designer()
Guido van Rossum
python.first_appeared()
1990; 29 years ago
p = python
p.designer()
Guido van Rossum
p.first_appeared()
1990; 29 years ago
Now creating an object is not a hard task all you have to do is just use a variable named “p” in this case and assign it to the class “python” and then with the help of a dot operator you can invoke the functions inside the class.
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def func(a):
print("Hello my dog's name is " + a.name + " and its age is " + a.age)
p1 = Dog("Danny", "10")
p1.func()
Hello my dog's name is Danny and its age is 10
Here in the above example, I am using the init() which is executed when the class is being initiated. Here every class contains the init() function. As seen above with the help of the self parameter we can access the variables inside the class.
class Dog:
breed = "Alaskan Malamute"
age = 10
d = Dog()
print(d.breed)
print("--------------------")
d.breed = "Labrador"
print(d.breed)
Alaskan Malamute -------------------- Labrador
You can delete an object by using the del keyword:
class Dog:
print("Alaskan Malamute")
print("10")
Alaskan Malamute 10
d = Dog()
print(d)
<__main__.Dog object at 0x7fe2b4028748>
del d
print(d)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-49-fae18cb68a55> in <module>() 1 del d ----> 2 print(d) NameError: name 'd' is not defined
You can delete properties on objects by using the del keyword:
class Dog:
name_of_the_dog = "Danny"
d = Dog
del d.name_of_the_dog
print(d.name_of_the_dog)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-50-ef7c0f660902> in <module>() 3 d = Dog 4 del d.name_of_the_dog ----> 5 print(d.name_of_the_dog) AttributeError: type object 'Dog' has no attribute 'name_of_the_dog'
To do this all you have to do is create an object of class type and using the dot operator access the name of the variable and the function of the class.
class dog:
age = 10
def name():
print("Danny")
d = dog
d.age
10
d.name()
Danny
I think now you guys believe that python is an objected oriented programming language. This is the end of the tutorial, I know this is a short tutorial, but this is enough for a good start to know more classes and objects. To read the official documentation of python classes I would recommend you guys to go through below:
In the upcoming tutorials, I will provide documentation about Inheritance, Parent and Child class and many more. Until then see you, Have a great day.