{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A Class" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Person object at 0x0000021D9EED60F0>\n" ] } ], "source": [ "\n", "class Person:\n", " pass # An empty block\n", "\n", "p = Person()\n", "print(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Method\n", "\n", "p = Person()\n", "p.speak() \n", "can also be written as\n", "Person().speak()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, how are you?\n" ] } ], "source": [ "class Person:\n", " def speak(self):\n", " print('Hello, how are you?')\n", "\n", "p = Person()\n", "p.speak()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# the __init__ method\n", "\n", "The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, my name is Carlos\n" ] } ], "source": [ "\n", "class Person:\n", " def __init__(self, name):\n", " self.name = name\n", "\n", " def speak(self):\n", " print('Hello, my name is', self.name)\n", "\n", "p = Person('Carlos')\n", "p.speak()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# self\n", "The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called. For example the below code is the same as the above code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Another class" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "class Pet(object):\n", "\n", " def __init__(self, name, species):\n", " self.name = name\n", " self.species = species\n", "\n", " def getName(self):\n", " return self.name\n", "\n", " def getSpecies(self):\n", " return self.species\n", "\n", " def __str__(self):\n", " return \"%s is a %s\" % (self.name, self.species)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# inheritance" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "class Dog(Pet):\n", "\n", " def __init__(self, name, chases_cats):\n", " Pet.__init__(self, name, \"Dog\")\n", " self.chases_cats = chases_cats\n", "\n", " def chasesCats(self):\n", " return self.chases_cats\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Cat(Pet):\n", "\n", " def __init__(self, name, hates_dogs):\n", " Pet.__init__(self, name, \"Cat\")\n", " self.hates_dogs = hates_dogs\n", "\n", " def hatesDogs(self):\n", " return self.hates_dogs\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "myPet = Pet(\"Boby\", \"Dog\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "myDog = Dog(\"Boby\", True)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(myDog, Pet)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(myDog, Dog)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(myPet, Pet)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(myPet, Dog)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }