Image for post
Image for post

An objected-oriented approach is most useful when your code involves complex interactions of many objects. In real production code, classes can have dozens of attributes & methods with complicated logic, but the underlying structure is the same as with the most simple class.

Classes are like a blueprint for objects outlining possible behaviors & states that every object of a certain type could have. For example, if you say, “Every customer will have a phone number & an e-mail, & will be able to place and cancel orders”, you just defined what a class really is. This way, you can talk about customers in a unified way. Then a specific customer object is just a realization of this class with a particular state value.

Image for post
Image for post

Finding Python Classes

In Python, everything is an object. Numbers, strings, DataFrames, even functions are objects. In particular, everything you deal with in Python has a class, a blueprint associated with it under the hood. The existence of these unified interfaces is why you can use any DataFrame in the same way.

You can call type() on any Python object to find out its class. For example, the class of a numpy array is actually called ndarray (for n-dimensional array).

Image for post
Image for post

Classes incorporate information about state & behavior. State information in Python is contained in attributes & behavior information in methods.

Attributes & Methods

Take a numpy array: you have already been using some of its methods and attributes! For example, every numpy array has an attribute "shape" that you can access by specifying the array's name followed by a dot and shape.

It also has methods like max and reshape which are also accessible via dot.

Image for post
Image for post

Creating Your First Class

In this example, you will create an empty class Employee. Then you will create an object emp of the class Employee by calling Employee().

Image for post
Image for post

RELATED LINKS

Written by

Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store