Skip to content
Snippets Groups Projects
6_Classes.ipynb 4.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    {
     "cells": [
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "On disait que \n",
        "* Python est un langage de programmation **interprété, multi-paradigme et multiplateformes**\n",
        "* Il favorise la **programmation impérative structurée, fonctionnelle et orientée objet**\n",
        "\n",
        "Source: https://fr.wikipedia.org/wiki/Python_(langage)"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "# Les classes"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 9,
       "metadata": {
        "collapsed": true
       },
       "outputs": [],
       "source": [
        "class Person:\n",
        "    \n",
        "    def __init__(self, name=None, surname=None, age=0):\n",
        "        \"\"\"Constructor\"\"\"\n",
        "        \n",
        "        self.name = name\n",
        "        self.surname = surname\n",
        "        self.age = age\n",
        "        \n",
        "    def today_is_your_birthday(self):\n",
        "        \n",
        "        self.age += 1\n",
        "        \n",
        "    def __str__(self):\n",
        "        \n",
        "        return \"\"\"\n",
        "        Name: %s\n",
        "        Surname: %s\n",
        "        Age: %i\n",
        "        \"\"\" % (self.name, self.surname, self.age)\n",
        "    \n",
        "    def __repr__(self):\n",
        "        \n",
        "        return \"Voici à quoi ça sert !\""
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 10,
       "metadata": {
        "collapsed": true
       },
       "outputs": [],
       "source": [
        "myPerson = Person(\"Michel\", \"Dupont\")"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 11,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "\n",
          "        Name: Michel\n",
          "        Surname: Dupont\n",
          "        Age: 0\n",
          "        \n"
         ]
        }
       ],
       "source": [
        "print(myPerson)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 12,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/plain": [
           "Voici à quoi ça sert !"
          ]
         },
         "execution_count": 12,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "myPerson"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 13,
       "metadata": {
        "collapsed": true
       },
       "outputs": [],
       "source": [
        "myPerson.today_is_your_birthday()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 14,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "\n",
          "        Name: Michel\n",
          "        Surname: Dupont\n",
          "        Age: 1\n",
          "        \n"
         ]
        }
       ],
       "source": [
        "print(myPerson)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 15,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/plain": [
           "True"
          ]
         },
         "execution_count": 15,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "isinstance(myPerson, Person)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 16,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/plain": [
           "['__class__',\n",
           " '__delattr__',\n",
           " '__dict__',\n",
           " '__dir__',\n",
           " '__doc__',\n",
           " '__eq__',\n",
           " '__format__',\n",
           " '__ge__',\n",
           " '__getattribute__',\n",
           " '__gt__',\n",
           " '__hash__',\n",
           " '__init__',\n",
           " '__le__',\n",
           " '__lt__',\n",
           " '__module__',\n",
           " '__ne__',\n",
           " '__new__',\n",
           " '__reduce__',\n",
           " '__reduce_ex__',\n",
           " '__repr__',\n",
           " '__setattr__',\n",
           " '__sizeof__',\n",
           " '__str__',\n",
           " '__subclasshook__',\n",
           " '__weakref__',\n",
           " 'age',\n",
           " 'name',\n",
           " 'surname',\n",
           " 'today_is_your_birthday']"
          ]
         },
         "execution_count": 16,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "dir(myPerson)"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "Pour plus d'info : https://python101.pythonlibrary.org/chapter11_classes.html"
       ]
      }
     ],
     "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.5.3"
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }