{ "cells": [ { "cell_type": "markdown", "id": "90cc6d0b", "metadata": {}, "source": [ "# Object-Oriented Programming\n", "\n", "Python is an object-oriented programming (OOP) language. In Python, just about everything is an “object”. \n", "\n", "Objects have their own attributes. Let’s say we have an object called `cat`. A cat's attributes could include color, size, and age. Suppose we want to know the color of the `cat`. We can inspect the color attribute like this:\n", "\n", "```\n", "cat.color \n", "```\n", "> red \n", "\n", "Objects also have their own methods, which are basically built-in functions that are applied to the object. In this case, the `cat`’s methods could include jumping, sleeping, or playing. This is how we would ask the cat to jump:\n", "\n", "```\n", "cat.jump()\n", "```" ] }, { "cell_type": "markdown", "id": "80b248d9", "metadata": {}, "source": [ "Now, you might be wondering: where did this `cat` object come from? How did we create it? \n", "\n", "An object is an instance of a \"[class](https://docs.python.org/3/tutorial/classes.html)\", which can be thought of as a “blueprint” for creating objects. That means that our object, `cat`, came from a class. Let's call the class `Cat`. The `Cat` class is where the attributes and methods are defined. It might look something like this:" ] }, { "cell_type": "code", "execution_count": 1, "id": "cb7d055d", "metadata": {}, "outputs": [], "source": [ "class Cat:\n", " def __init__(self, name, color, age):\n", " self.name = name\n", " self.color = color \n", " self.age = age\n", " \n", " def jump(self):\n", " print(\"jump!\")\n", "\n", " def meow(self):\n", " print(\"meow!\")" ] }, { "cell_type": "markdown", "id": "f7773053", "metadata": {}, "source": [ "The `cat` object was created like this:" ] }, { "cell_type": "code", "execution_count": 2, "id": "ae8ec5cf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "meow!\n" ] } ], "source": [ "cat = Cat(name='Tabby', color='red', age=2)\n", "cat.meow()" ] }, { "cell_type": "markdown", "id": "1472445e", "metadata": {}, "source": [ "As we'll learn very soon, all objects have a datatype. The datatype of an object is its class. In the case of our `cat` object, it's datatype is `Cat`! " ] }, { "cell_type": "markdown", "id": "1276db4b", "metadata": {}, "source": [ "```{note}\n", "When we start learning about dataframes in the next chapter, it'll be helpful to remember 2 things:\n", "\n", "- a dataframe attribute looks like: `dataframe.attribute_name` (without parentheses)\n", "- a dataframe method looks like: `dataframe.method()` (with parentheses)\n", "\n", "If this is super confusing, don't worry! We will learn as we go. \n", "```" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.9" } }, "nbformat": 4, "nbformat_minor": 5 }