Newer
Older
{
"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",
"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",
"outputs": [],
"source": [
"myPerson = Person(\"Michel\", \"Dupont\")"
]
},
{
"cell_type": "code",
"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",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Voici à quoi ça sert !"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myPerson"
]
},
{
"cell_type": "code",
"outputs": [],
"source": [
"myPerson.today_is_your_birthday()"
]
},
{
"cell_type": "code",
"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",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isinstance(myPerson, Person)"
]
},
{
"cell_type": "code",
"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']"
]
},
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"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",
}
},
"nbformat": 4,
"nbformat_minor": 2
}