{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Is Python object-oriented?.ipynb", "provenance": [], "collapsed_sections": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "QfPHMX1yhlpd", "colab_type": "text" }, "source": [ "# Is Python object-oriented?\n" ] }, { "cell_type": "markdown", "metadata": { "id": "6fHRMLXHhnhf", "colab_type": "text" }, "source": [ "## This tutorial answers the question of whether python is objected oriented or not?\n", "\n", "![alt text](https://miro.medium.com/max/902/1*2uZaR_zHJ_ccCzyjXJsXRQ.jpeg)" ] }, { "cell_type": "markdown", "metadata": { "id": "mpHHXq7phuYc", "colab_type": "text" }, "source": [ "\n", "\n", "\n", "> Well Is Python an object oriented programming language? Yes, it is. With the exception of control flow, everything in Python is an object. By Max Fischer, who answers this question in [Quora](https://www.quora.com/Is-Python-object-oriented).\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "EFffARQxiG1P", "colab_type": "text" }, "source": [ "## Its time to answer the above question in my style.\n", "\n", "Yes, python is an objected oriented programming language. Everything in python is an object. Using python we can create classes and objects. Here in this tutorial, I would be discussing only about classes and objects.\n", "\n", "\n", "> **Classes:** Class is like a blueprint that helps to create an object. In other words, a class comprises variables, methods, functions. Or you can refer class which contains properties and behavior. For example Consider a class “Ball” now the properties of Ball might be color, diameter, price and the behavior of the ball might be rolling, bouncing.\n", "\n", "\n", "\n", "\n", "> **Objects:** Objects are an instance of a class. With the help of objects, we can access the method and function of a class.\n", "\n", "\n", "\n", "\n", "## Now let us see how to create a class and an object in python\n", "\n", "Creating a class in Python is trivial, all you have to do is use a keyword called “class” along with a name which is often referred to as the name of the class. Inside the class, we can either declare functions or variables." ] }, { "cell_type": "code", "metadata": { "id": "JN3igx8ksnOk", "colab_type": "code", "colab": {} }, "source": [ "class python:\n", " def designer():\n", " print(\"Guido van Rossum\")\n", " def first_appeared():\n", " print(\"1990; 29 years ago\")" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "P2n9XDQ2s3Ob", "colab_type": "text" }, "source": [ "In order to access the function inside the class all you have to do call the class with its class name and with the “.” dot operator call its function. We can access the class in two ways: Without creating the object and creating an object" ] }, { "cell_type": "markdown", "metadata": { "id": "kGsXVGJBs3-R", "colab_type": "text" }, "source": [ "## Without creating an Object\n", "\n", "Just use the class name along with the dot operator to access the functions inside the class." ] }, { "cell_type": "code", "metadata": { "id": "jd9QnMyatEPu", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "585eb45a-02e5-4b67-be03-9643ce37a935" }, "source": [ "python.designer()" ], "execution_count": 41, "outputs": [ { "output_type": "stream", "text": [ "Guido van Rossum\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "34eCovxbtGFo", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "62d67229-1843-49bd-c38b-fde23f634595" }, "source": [ "python.first_appeared()" ], "execution_count": 42, "outputs": [ { "output_type": "stream", "text": [ "1990; 29 years ago\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "y8uDC6A6tIiX", "colab_type": "text" }, "source": [ "## Creating an object\n" ] }, { "cell_type": "code", "metadata": { "id": "3RSwWhkftO-G", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "0e5ea9e0-8f18-440c-d99a-c29c0b744f19" }, "source": [ "p = python\n", "p.designer()" ], "execution_count": 43, "outputs": [ { "output_type": "stream", "text": [ "Guido van Rossum\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "IyCQexFTtQ8D", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "84782f3d-a6d9-40b7-a08c-a8dd19caf146" }, "source": [ "p.first_appeared()" ], "execution_count": 44, "outputs": [ { "output_type": "stream", "text": [ "1990; 29 years ago\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "_D4JdXuvtWZf", "colab_type": "text" }, "source": [ "Now creating an object is not a hard task all you have to do is just use a variable named “p” in this case and assign it to the class “python” and then with the help of a dot operator you can invoke the functions inside the class.\n", "\n", "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "f-333feRtamj", "colab_type": "text" }, "source": [ "## The Self Parameter\n", "\n", "\n", "\n", "> The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class. Credits — [w3schools](https://www.w3schools.com/python/python_classes.asp)\n", "\n", "\n", "You can name the self parameter anything you like, its not mandatory to call it as self." ] }, { "cell_type": "code", "metadata": { "id": "lEI1D9NHtpPJ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "15170902-0629-49b8-bc39-51fd149cae48" }, "source": [ "class Dog:\n", " def __init__(self, name, age):\n", " self.name = name\n", " self.age = age\n", " def func(a):\n", " print(\"Hello my dog's name is \" + a.name + \" and its age is \" + a.age)\n", "p1 = Dog(\"Danny\", \"10\")\n", "p1.func()" ], "execution_count": 45, "outputs": [ { "output_type": "stream", "text": [ "Hello my dog's name is Danny and its age is 10\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "XKb4GyAJt25E", "colab_type": "text" }, "source": [ "Here in the above example, I am using the init() which is executed when the class is being initiated. Here every class contains the init() function. As seen above with the help of the self parameter we can access the variables inside the class." ] }, { "cell_type": "markdown", "metadata": { "id": "ZnZ3RrUwt4sL", "colab_type": "text" }, "source": [ "## We can also access and modify the variables inside the class without the self parameter." ] }, { "cell_type": "code", "metadata": { "id": "FkVl1GU2t78r", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "e7f0f513-1530-4588-ee11-188add468cc7" }, "source": [ "class Dog:\n", " breed = \"Alaskan Malamute\"\n", " age = 10\n", "d = Dog()\n", "print(d.breed)\n", "print(\"--------------------\")\n", "d.breed = \"Labrador\"\n", "print(d.breed)" ], "execution_count": 46, "outputs": [ { "output_type": "stream", "text": [ "Alaskan Malamute\n", "--------------------\n", "Labrador\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "NfXyTpvXt_FU", "colab_type": "text" }, "source": [ "## Lets us see how can we delete the object using the “del” keyword.\n", "\n", "You can delete an object by using the del keyword:" ] }, { "cell_type": "code", "metadata": { "id": "AqSlIQecuCvw", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 52 }, "outputId": "5c42bb77-a1f2-44d9-cb59-37a112b0e5e3" }, "source": [ "class Dog:\n", " print(\"Alaskan Malamute\")\n", " print(\"10\")" ], "execution_count": 47, "outputs": [ { "output_type": "stream", "text": [ "Alaskan Malamute\n", "10\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "wM3H5zi9uHcK", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "4ae3e97e-f0f5-4bc2-b2a6-3e67a4c8ae98" }, "source": [ "d = Dog()\n", "print(d)" ], "execution_count": 48, "outputs": [ { "output_type": "stream", "text": [ "<__main__.Dog object at 0x7fe2b4028748>\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "fo9kPdPLuJnj", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 183 }, "outputId": "ca805dcf-3c00-430e-b9b6-c0b894441b93" }, "source": [ "del d\n", "print(d)" ], "execution_count": 49, "outputs": [ { "output_type": "error", "ename": "NameError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0md\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'd' is not defined" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "FR1rp40ZuLIJ", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "rIj0tKpeuL3m", "colab_type": "text" }, "source": [ "## Let us see how to delete the properties of the object\n", "\n", "You can delete properties on objects by using the del keyword:" ] }, { "cell_type": "code", "metadata": { "id": "Rku4l2inuPgR", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 201 }, "outputId": "89ed886d-ac0e-4b71-86b2-f6a835ee78a4" }, "source": [ "class Dog:\n", " name_of_the_dog = \"Danny\"\n", "d = Dog\n", "del d.name_of_the_dog\n", "print(d.name_of_the_dog)" ], "execution_count": 50, "outputs": [ { "output_type": "error", "ename": "AttributeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDog\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname_of_the_dog\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname_of_the_dog\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: type object 'Dog' has no attribute 'name_of_the_dog'" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "tmdinWmAuTL8", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "gOMgRO47uT9J", "colab_type": "text" }, "source": [ "## Accessing object variables and function inside a class\n", "\n", "To do this all you have to do is create an object of class type and using the dot operator access the name of the variable and the function of the class." ] }, { "cell_type": "code", "metadata": { "id": "a-qcvGvauXYz", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "38f79a68-8190-4156-b1d6-6bf46d38bcf7" }, "source": [ "class dog:\n", " age = 10\n", " def name():\n", " print(\"Danny\")\n", "d = dog\n", "d.age" ], "execution_count": 51, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "10" ] }, "metadata": { "tags": [] }, "execution_count": 51 } ] }, { "cell_type": "code", "metadata": { "id": "OIUIsRUNuZRr", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "5d023b59-9551-4ce0-d4c5-84a7680577a0" }, "source": [ "d.name()" ], "execution_count": 52, "outputs": [ { "output_type": "stream", "text": [ "Danny\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "39gMGmpFua3C", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "IIZ-35cOubl-", "colab_type": "text" }, "source": [ "I think now you guys believe that python is an objected oriented programming language. This is the end of the tutorial, I know this is a short tutorial, but this is enough for a good start to know more classes and objects. To read the official documentation of python classes I would recommend you guys to go through below:\n", "\n", "[Python Classes](https://docs.python.org/3/tutorial/classes.html?source=post_page-----834d6c70cf54----------------------)\n", "\n", "In the upcoming tutorials, I will provide documentation about Inheritance, Parent and Child class and many more. Until then see you, Have a great day.\n", "\n", "\n", "---\n", "\n" ] } ] }