Saturday, August 10, 2019

ListenData: Python : Learn Object Oriented Programming in 3 Hours

This tutorial outlines object oriented programming (OOP) in Python with examples. It is a step by step guide which was designed for people who have no programming experience. Object Oriented Programming is popular and available in other programming languages besides Python which are Java, C++, PHP.
Table of Contents

What is Object Oriented Programming?

In object-oriented programming (OOP), you have the flexibility to represent real-world objects like car, animal, person, ATM etc. in your code. In simple words, an object is something that possess some characteristics and can perform certain functions. For example, car is an object and can perform functions like start, stop, drive and brake. These are the function of a car. And the characteristics are color of car, mileage, maximum speed, model year etc.

In the above example, car is an object. Functions are called methods in OOP world. Characteristics are attributes (properties). Technically attributes are variables or values related to the state of the object whereas methods are functions which have an effect on the attributes of the object.

In Python, everything is an object. Strings, Integers, Float, lists, dictionaries, functions, modules etc are all objects.
OOP Python

Do Data Scientists Use Object Oriented Programming?

It's one of the most common question data scientists have before learning OOP. When it comes to data manipulation and machine learning using Python, it is generally advised to study pandas, numpy, matplotlib, scikit-learn libraries. These libraries were written by experienced python developers to automate or simplify most of tasks related to data science. All these libraries depend on OOP and its concepts. For example, you are building a regression model using scikit-learn library. You first have to declare your model as an object and then you use a fit method. Without knowing fundamentals of OOP, you would not be able to understand why you write the code in this manner.

In python, there are mainly 3 programming styles which are Object-Oriented Programming, Functional Programming and Procedural Programming. In simple words, there are 3 different ways to solve the problem in Python. Functional programming is most popular among data scientists as it has performance advantage. OOP is useful when you work with large codebases and code maintainability is very important.

Conclusion : It's good to learn fundamentals of OOP so that you understand what's going behind the libraries you use. If you aim to be a great python developer and want to build Python library, you need to learn OOP (Must!). At the same time there are many data scientists who are unaware of OOP concepts and still excel in their job.

Basics : OOP in Python

In this section, we will see concepts related to OOP in Python in detail.

Object and Class

Class is a architecture of the object. It is a proper description of the attributes and methods of a class. For example, design of a car of same type is a class. You can create many objects from a class. Like you can make many cars of the same type from a design of car.

There are many real-world examples of classes as explained below -

  • Recipe of Omelette is a class. Omelette is an object.
  • Bank Account Holder is a class. Attributes are First Name, Last Name, Date of Birth, Profession, Address etc. Methods can be "Change of address", "Change of Profession", " Change of last name" etc. "Change of last name" is generally applicable to women when they change their last name after marriage
  • Dog is a class. Attributes are Breed, Number of legs, Size, Age, Color etc. Methods can be Eat, Sleep, Sit, Bark, Run etc.

In python, we can create a class using the keyword class. Method of class can be defined by keyword def. It is similar to a normal function but it is defined within a class and is a function of class. The first parameter in the definition of a method is always self and method is called without the parameter self.

READ MORE »

from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...