2024-heraklion-scientific-p.../notebooks/01b_Classes_teacher_edition.ipynb
2024-08-27 15:52:41 +03:00

280 lines
5.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The smell of classes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The \"data bundle\" smell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-11T12:50:34.342224Z",
"start_time": "2021-08-11T14:50:34.336560+02:00"
}
},
"outputs": [],
"source": [
"def momentum(mass, velocity):\n",
" return mass * velocity\n",
"\n",
"def energy(mass, velocity):\n",
" return 0.5 * mass * velocity ** 2\n",
"\n",
"def update_position(velocity, position, dt):\n",
" return position + velocity * dt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-11T12:50:34.776689Z",
"start_time": "2021-08-11T14:50:34.769617+02:00"
}
},
"outputs": [],
"source": [
"# Naive\n",
"mass1 = 10.0\n",
"velocity1 = 0.9\n",
"\n",
"mass2 = 12.0\n",
"velocity2 = 0.1\n",
"\n",
"print(momentum(mass1, velocity1))\n",
"print(momentum(mass2, velocity2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have two parameters that will be sent to these functions over and over again: `mass` and `velocity`.\n",
"\n",
"Moreover, the parameters cannot be mixed up (e.g. the velocity of one particle with the mass of another)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-07-27T15:05:52.548364Z",
"start_time": "2018-07-27T17:05:52.544726+02:00"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introducing classes as a data bundle template"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-04T13:04:49.208543Z",
"start_time": "2018-08-04T15:04:49.203180+02:00"
}
},
"outputs": [],
"source": [
"class Particle:\n",
" pass\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Class methods"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-07-27T12:13:56.972938Z",
"start_time": "2018-07-27T14:13:56.969323+02:00"
}
},
"outputs": [],
"source": [
"def momentum(particle):\n",
" return particle.mass * particle.velocity\n",
"\n",
"print(momentum(particle1))\n",
"print(momentum(particle2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"class Particle:\n",
" def __init__(self, mass, velocity):\n",
" self.mass = mass\n",
" self.velocity = velocity\n",
"\n",
" # Method here\n",
"\n",
"particle1 = Particle(10.0, 0.9, 0.0)\n",
"print(particle1.momentum())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"We have been using class instances and methods all along..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"s = 'A scanner Darkly'\n",
"s.capitalize()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"x = set(['apple', 'banana', 'apple', 'pineapple'])\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"x.union(['banana', 'kiwi'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"\n"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.3"
},
"toc": {
"nav_menu": {
"height": "174px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_position": {
"height": "953px",
"left": "0px",
"right": "1253px",
"top": "127px",
"width": "320px"
},
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}