{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains material from [CBE40455-2020](https://jckantor.github.io/CBE40455-2020);\n", "content is available [on Github](https://github.com/jckantor/CBE40455-2020.git).*\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [3.0 Introduction to Discrete Event Simulation](https://jckantor.github.io/CBE40455-2020/03.00-Introduction-to-Discrete-Event-Modeling.html) | [Contents](toc.html) | [3.2 Introduction to Simpy](https://jckantor.github.io/CBE40455-2020/03.02-Introduction-to-Simpy.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[3.1 Python Generators](https://jckantor.github.io/CBE40455-2020/03.01-Python-Generators.html#3.1-Python-Generators)",
"section": "3.1 Python Generators"
}
},
"source": [
"# 3.1 Python Generators"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[3.1.1 A simple simulation model](https://jckantor.github.io/CBE40455-2020/03.01-Python-Generators.html#3.1.1-A-simple-simulation-model)",
"section": "3.1.1 A simple simulation model"
}
},
"source": [
"## 3.1.1 A simple simulation model\n",
"\n",
"As a simple demonstration, let's consider the task of modeling the progess of a disease in a single person. The person will go through several stages of the disease\n",
"\n",
"$$\\text{Uninfected} \\quad \\longrightarrow \\quad \\text{Exposed} \\quad \\longrightarrow \\quad \\text{Infectious} \\quad \\longrightarrow \\quad \\text{Recovered}$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[3.1.1.1 Python generators](https://jckantor.github.io/CBE40455-2020/03.01-Python-Generators.html#3.1.1.1-Python-generators)",
"section": "3.1.1.1 Python generators"
}
},
"source": [
"### 3.1.1.1 Python generators\n",
"\n",
"Python generators are the basic building block for discrete event simulation in Python. Python generators are similar to functions with three profound differences:\n",
"\n",
"1. They return values using the `yield` statement rather than a `return` statement. They don't disappear from memory until all statements have been executed.\n",
"2. Because they don't disappear, they can start and be restarted with a `next` statement.\n",
"3. Multiple instances of a generator may be working at the same time."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"nbpages": {
"level": 3,
"link": "[3.1.1.1 Python generators](https://jckantor.github.io/CBE40455-2020/03.01-Python-Generators.html#3.1.1.1-Python-generators)",
"section": "3.1.1.1 Python generators"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def a_function():\n",
" return 3\n",
"\n",
"a_function()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"nbpages": {
"level": 3,
"link": "[3.1.1.1 Python generators](https://jckantor.github.io/CBE40455-2020/03.01-Python-Generators.html#3.1.1.1-Python-generators)",
"section": "3.1.1.1 Python generators"
}
},
"outputs": [
{
"data": {
"text/plain": [
"
"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}