![]() Here, "Mountain Bike" is passed to the name parameter of _init_(). If we use a constructor to initialize values inside a class, we need to pass the corresponding value during the object creation of the class. We have used the self.name to refer to the name attribute of the bike1 object. The constructor above initializes the value of the name attribute. Here, _init_() is the constructor function that is called whenever a new object of that class is instantiated. However, we can also initialize values using the constructors. Finally, the statement inside the method is executed.Įarlier we assigned a default value to a class attribute, class Bike: Notice that we have also used the object to call the method inside the class, study_room.calculate_area() We then used the object to assign values to attributes: length and breadth. Here, we have created an object named study_room from the Room class. In the above example, we have created a class named Room with: The new class that is created is known as subclass (child or derived class) and the existing class from which the child class is derived is known as superclass (parent or base class). Inheritance allows us to create a new class from an existing class. Print("Area of Room =", self.length * self.breadth) Like any other OOP languages, Python also supports the concept of class inheritance. A Python Function defined inside a class is called a method. We can also define a function inside a Python class. In the above example, we have created two objects employee1 and employee2 of the Employee class. # access attributes and assign new values Here, we have used bike1.name and bike1.gear to change and access the value of name and gear attribute respectively.Įxample 1: Python Class and Objects # define a class notation to access the attributes of a class. Now, we can use this object to access the class attributes. For example, suppose Bike is a class then we can create objects like bike1, bike2, etc from the class. Note: The variables inside a class are called attributes.Īn object is called an instance of a class. name/gear - variables inside the class with default values "" and 0 respectively.Here, we have created a class named ClassName. We use the class keyword to create a class in Python. Since many houses can be made from the same description, we can create many objects from a class. Based on these descriptions we build the house. A child class inherits methods and variables from the parent class. It contains all the details about the floors, doors, windows, etc. Inheritance is the ability from one class to inherit from a parent class. ![]() We can think of the class as a sketch (prototype) of a house. Similarly, a class is a blueprint for that object.īefore we learn about objects, let's first know about classes in Python.Ī class is considered as a blueprint of objects. We know that python also supports the concept of objects and classes.Īn object is simply a collection of data (variables) and methods (functions). In the last tutorial, we learned about Python OOP. ![]()
0 Comments
Leave a Reply. |