In this article you will learn python object and classes
In this article you will learn about Python Object and Classes.
Start learning Python from scratch for Free.
- Introduction to Programming – Day1
- Introduction to Algorithms – Day 2
- Python operators and variables – Day 3
- Python Control Structures – Day 4
- Functions in Python – Day 5 of Python Series
- How to setup Eclipse IDE for Python? – 6th Day of Python Series
- Collection Data types in Python – 7th Day of Python Series
- Python Debugging and Testing –Python Series-8th day
- Python Built-in Functions, Libraries, and modules – Day 9
- Python regular expressions and lambda functions – Day 10
- Introduction of Python Object-oriented programming(Python Oops) – Day 11
- Introduction of Python Object-oriented programming(Python Oops) – Day 12
Objects are real-world entities. Anything you can describe in this world is an object. Classes on the other hand are not real. They are just a concept. Class is a short form of Classification. A class is a classification of certain objects and it is just a description of the properties and behavior all objects of that classification should possess.
Object-oriented programming stresses on Object.
Objects are real-world entities, we can easily relate objects with real world.
Class in Python
Class can be defined as a blueprint for making objects. just like civil engineers make blueprint before making a house.
What does the class contain?
As a recipe contains a list of ingredients that make up the cake and the directions. Similarly, a class contains the properties/attributes of the object and the operations/behavior that can be performed on an object.
How to define class in Python?
A class is defined using the class keyword in python.
#This is Python Example class
Python Built-in Classes
Python has som built in classes which we had used before.
Object in Python
Objects in programming are relatable to the real world. Objects are created to solve real-world problems.
- Is sound an Object?
- Is light an Object?
- Yes, Sound is an object because it has attributes like Frequency, Wavelength, and behavior like reflection transition.
- Yes, light is an object because it has attributes like Pitch, Wavelength, and behavior like reflection transition.
How to create objects in python
To create an object, we need a class. The syntax for creating an object is “<classname>()”, where <classname> is the name of the class.
Objects of example class
Here we created two objects of class Example.
Objects are created but we can not access them because we have not assigned them to any variable.
mob1=Mobile() mob2=Mobile() mob3=Mobile()
Now we can access the objects and reuse them.
Attributes of an object.
This can be done by using the . (dot) operator. The syntax for creating attribute and value for that is as below:
class Mobile: pass mob1=Mobile() mob2=Mobile() mob1.price=20000 mob1.brand="Apple" mob2.price=3000 mob2.brand="Samsung"
Accessing Attribute value
We can access the attribute values using the dot operator itself. The syntax is as shown below:
print (mob1.brand) print (mob2.brand)
we have to deal with the problem of lack of reuse. For example if an object has 10 attributes and we have 10 objects, then we have to write 100 lines of code! There is no reuse at all.
We need a way to mention the attributes of all the objects of a class in one place so that we can create and initialize the attributes. Let us see how to do this.
How to make attributes in a class?
Attributes can be added to a class through a special function called __init__(). We will discuss more about the syntax later. But for now, this is how the mobile class will look like with attributes in it.When we create an object, the special __init__() function inside the class of that object is invoked automatically. This special function is called as a constructor. Try out the below code and observe the output.
How to add behaviour in a class
We can create behavior in a class by adding functions in a class. However, such functions should have a special parameter called self as the first parameter. We will discuss more about the syntax later on. This is how the purchase behavior in a mobile class would look like:
class Mobile: def __init__(self): print("Inside constructor") def purchase (self): print("Purchasing a mobile") mob1=Mobile() mob1.purchase()
What is Abstraction in python?
When we invoke the purchase() on a mobile object, we don’t have to know the details of the method to invoke it. We don’t have to know how the reverse() method is working in order to use it in our list.
This ability to use something without having to know the details of how it is working is called abstraction.