What is OOP?
1. What is OOP?
Hi! I'm Alex, and I'll be teaching you to think object-oriented!2. Procedural vs OOP
Until now, you have probably been coding in so-called procedural style: your code was a sequence of steps to be carried out. This is common when doing data analysis: you download the data, process, and visualize it.3. Thinking in sequences
Procedural thinking is natural. You get up, have breakfast, go to work. This sequential viewpoint works great if you are trying to plan your day. But if you are a city planner, you have to think about thousands of people with their own routines. Trying to map out a sequence of actions for each individual would quickly get unsustainable. Instead, you are likely to start thinking about patterns of behaviors. Same thing with code. The more data it uses, the more functionality it has, the harder it is to think about as just a sequence of steps.4. Procedural vs OOP
Instead, we view it it as a collection of objects, and patterns of their interactions - like users interacting with elements of an interface. This point of view becomes invaluable when designing frameworks, like application program interfaces or graphical user interfaces, or building tools like pandas DataFrames! It will help you organize your code better, making it more reusable and maintainable.5. Objects as data structures
The fundamental concepts of OOP are objects and classes. An object is a data structure incorporating information about state and behavior. For example, an object representing a customer can have a certain phone number and email associated with them, and behaviors like placeOrder or cancelOrder. An object representing a button on a website can have a label, and can triggerEvent when pressed. The distinctive feature of OOP is that state and behavior are bundled together: instead of thinking of customer data separately from customer actions, we think of them as one unit representing a customer. This is called encapsulation, and it's one of the core tenets of object-oriented programming.6. Classes as blueprints
The real strength of OOP comes from utilizing classes. Classes are like blueprints for objects. They describe the possible states and behaviors that every object of a certain type could have. For example, if you say "every customer will have a phone number and an email, and will be able to place and cancel orders", you just defined a class! This way, you can talk about customers in a unified way.7. Classes as blueprints
Then a specific Customer object is just a realization of this class with particular state values.8. Objects in Python
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, for example, 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).9. Attributes and methods
Classes incorporate information about state and behavior. State information in Python is contained in attributes, and behavior information -- in methods. Take a numpy array: you've 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 name of the array, then dot, and shape. It also has methods, like max and reshape, which are also accessible via dot.10. Object = attributes + methods
Attributes (or states) in Python objects are represented by variables -- like numbers, or strings, or tuples, in the case of the numpy array shape. Methods, or behaviors, are represented by functions. Both are accessible from an object using the dot syntax. You can list all the attributes and methods that an object has by calling dir() on it. For example here, we see that a numpy array has methods like trace and transpose.11. Let's review!
Let's stop here for now. Don't worry if it seems confusing. It's a different way of thinking, and it will take some time to get used to. Now head over to the exercises to review what you learned!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.