In our example, to use the Airplane class, one only needs to know about the add_passenger and list_passengers methods, without having to know about the internal representation of the airplane object. AbstractionĪbstraction refers to separating the implementation details of a class from the objects it represents, so that objects can be manipulated without having to know about their underlying implementation. The way we extract or interact with the attributes of the airplane object is via the two methods we defined, ` add_passenger` and ` list_passengers`. This object now contains the attributes of the plane. In the above example, we “encapsulated” the details of an airplane in an object (an instance of Airplane class). It refers to hiding the implementation details of a class from the outside world and exposing only the necessary information through a class’s interface (i.e., its public methods). It is also known as the constructor and is used to initialize the object’s attributes.Įncapsulation is a core principle in OOP. Simply put, an object is an item created based on the rules and framework defined by the class.Ĭonstructor: The init method is a special method that is called automatically when an object is created. In the above example, we created an “object” called airplane which was an instance of the class Airplane. Objects/InstancesĪn object is an individual instance of a class. This example shows how OOP can be used to model real-world objects in code. The class has methods for adding passengers to the list and listing the current passengers. The class has attributes for the make, model, and capacity of the airplane, as well as a list of passengers. In this example, the Airplane class encapsulates the data and behavior of an airplane. passengers: print (passenger) # Create an instance of the Airplane class airplane = Airplane( " Boeing ", " 747 ", 400 ) # Add passengers to the passenger list airplane.add_passenger( " John Doe " ) airplane.add_passenger( " Jane Doe " ) # List the current passengers airplane.list_passengers() Python passengers.append(name) # Method for listing the current passengers def list_passengers ( self ): print ( " Passengers: " ) for passenger in self. passengers = # Method for adding a passenger to the passenger list def add_passenger ( self, name ): self. Example: Use of Class in PythonĬlass Airplane : # Initialize the attributes for the airplane def _init_ ( self, make, model, capacity ): self. For example, we can define a class “Airplane” with attributes “make”, “model”, “capacity” and a method “add_passenger” that adds a passenger to the plane’s passenger list. A class defines a set of attributes (data) and behaviors (functions) that are common to all instance attributes of that class. instances) with specific attributes and methods. ClassesĪ class is a blueprint for creating objects (i.e. Basic Concepts of Object-Oriented Programming (OOP)īefore we get into the specifics, let’s get a high-level overview of the foundational concepts of OOP – classes, objects, inheritance, polymorphism, and encapsulation. In this tutorial, we will cover the basics of OOP in Python and how it is used in the data science workflow. While OOP may not be applicable to every project that a Data Scientist takes on, it can be specifically beneficial in projects with complex data structures and, in general, a large code base. However, Data Scientists can also leverage OOP to create maintainable, modular, scalable and reusable code. Data Scientists however have been slow to adopt due to moderate complexity and lack of formal education and focus on OOP in the Data Science field. OOP is mostly popular among software engineers as their jobs demand scalable and modular code. OOP is a programming paradigm that allows you to model real-world objects, their behaviors, their interactions with similar and other objects, making it a powerful tool for organizing and manipulating data in a clear and manageable way. This is where Object Oriented Programming (OOP) can help you build efficient set of code to to manage. Each airplane has its own set of attributes, such as make, model, identifier, seating capacity, as well as actions such as route, schedule etc. Imagine you’re a data scientist tasked with optimizing the flight patterns of a fleet of airplanes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |