{ "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": 1, "metadata": {}, "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 f\"\"\"\n", " Name: {self.name}\n", " Surname: {self.surname}\n", " Age: {self.age}\n", " \"\"\"\n", "\n", " def __repr__(self):\n", " \n", " return \"Voici à quoi ça sert !\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "myPerson = Person(\"Michel\", \"Dupont\")" ] }, { "cell_type": "code", "execution_count": 3, "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": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Voici à quoi ça sert !" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myPerson" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "myPerson.today_is_your_birthday()" ] }, { "cell_type": "code", "execution_count": 6, "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": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(myPerson, Person)" ] }, { "cell_type": "code", "execution_count": 8, "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", " '__init_subclass__',\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": 8, "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.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }