As you probably may knows Python is an Object-Oriented Programming Language, meaning you can treat different entities in program as Objects – Bundle of Data and Methods which can be applied on Data. As Object contain both data and methods which can be called upon it, these can be really useful. Because when you define the object, you can also define methods which can be called upon it at the same time.(Interesting!!)
Now as you know objects can be useful, let’s see How to create Objects in Python? Objects in Python can be created using Classes, which contain definition of data and methods for that specific type of object. For example – If there’s a class let’s say Car – This class would contain Data about Car and methods(Like Move Forward, Move Backward, Accelerate).
Now if you want to create let’s a Tesla Car, what would you do? Just take Car Class and make an Object Car and then assign some features to Car Object which are specific to Tesla cars. Quite Simple.
Classes are kind of framework/procedure for creating objects, Classes also contain some fundamental data and methods specific to it. Classes are ingredient for making dishes. 😂
How to create Class in Python?
As I’ve mentioned above any class typically contain two things – Data and Methods. So let’s see step by step and make a Python Class.
- Use Keyword class with some name for initiating a newer class
- Define what data class will contain inside def __init__ method
- Using def function_name(parameters) initialise some class methods
That’s it, now you got a Class. Let’s follow this procedure with an example so that you can better get an idea about What I’m talking.
Use Keyword class with some name for initiating a newer class
Let’s say you want to create a Car Class, so first step would be to write class Car:
Define what data class will contain inside def __init__ method
After writing class Car: we need to define def __init__(some parameters) which will be initialising data.
class Car: def __init__(self, name, model): self.name = name self.model = model
Here self keyword represents instance of class, it’s kind of giving the car an arbitrary name while its in process of manufacturing in factory.
Also def __init__ is often called Constructor of Class, as its initialising Class itself.
Using def function_name(parameters) initialise some class methods
Any class or object contains Methods, so that its easier to access Data into Class/Object itself. It’s like have an About Phone section in your Smartphone’s Setting, which shows information about Phone itself.
Similarly these methods can be called upon object itself to get what Data it contains or these methods can also be used for adding/updating data contained by Object.
class Car: def __init__(self, name, model): self.name = name self.model = model def name(self): return self.name def model(self): return self.model
How to create an object from Class in Python?
Object from a Class in Python can be created using by just passing required parameters in circular brackets. For example – For making an object from Class Car, do Car(“Tesla”, “Model S”). This will create an object, you can also assign this newly created object to some variable name tesla_car = Car(“Tesla”, “Model S”).
Do note that if an class expects 2 parameters then passing less or more than two parameters will raise TypeError. So just make sure your passing right number of parameters to avoid TypeErrors being raised by Python. 😀