Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created February 16, 2025 09:07
Show Gist options
  • Save mahenzon/d73bc071482adcb43a6e314fe8f89801 to your computer and use it in GitHub Desktop.
Save mahenzon/d73bc071482adcb43a6e314fe8f89801 to your computer and use it in GitHub Desktop.
Python functools.partial examples
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:51:03.869147Z",
"start_time": "2025-01-18T15:51:03.864894Z"
}
},
"cell_type": "code",
"source": [
"def multiply(x, y):\n",
" return x * y"
],
"id": "7daf5567e4526f98",
"outputs": [],
"execution_count": 1
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:51:07.450625Z",
"start_time": "2025-01-18T15:51:07.445103Z"
}
},
"cell_type": "code",
"source": "multiply(2, 3)",
"id": "1c9b7a7100c82e97",
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 2
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:51:20.831614Z",
"start_time": "2025-01-18T15:51:20.828801Z"
}
},
"cell_type": "code",
"source": [
"print(multiply(2, 3))\n",
"print(multiply(2, 4))\n",
"print(multiply(2, 5))"
],
"id": "72d57525a55b44a9",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"8\n",
"10\n"
]
}
],
"execution_count": 3
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:51:54.840788Z",
"start_time": "2025-01-18T15:51:54.838891Z"
}
},
"cell_type": "code",
"source": "from functools import partial",
"id": "324d66f7d7c26048",
"outputs": [],
"execution_count": 4
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:52:10.799357Z",
"start_time": "2025-01-18T15:52:10.797019Z"
}
},
"cell_type": "code",
"source": [
"double = partial(multiply, 2)\n",
"print(double)"
],
"id": "2c7521ebd6e4f172",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"functools.partial(<function multiply at 0x1045a1620>, 2)\n"
]
}
],
"execution_count": 5
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:52:36.951047Z",
"start_time": "2025-01-18T15:52:36.948692Z"
}
},
"cell_type": "code",
"source": "double(3)",
"id": "aae221e14318e1",
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 6
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:52:55.241475Z",
"start_time": "2025-01-18T15:52:55.237132Z"
}
},
"cell_type": "code",
"source": [
"print(double(4))\n",
"print(double(5))"
],
"id": "c18f30e2b97acea",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"10\n"
]
}
],
"execution_count": 7
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:53:26.988555Z",
"start_time": "2025-01-18T15:53:26.985061Z"
}
},
"cell_type": "code",
"source": [
"triple = partial(multiply, 3)\n",
"\n",
"print(triple(2))\n",
"print(triple(3))\n",
"print(triple(4))"
],
"id": "1edf2cbd0d70e62e",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"9\n",
"12\n"
]
}
],
"execution_count": 8
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:54:49.697476Z",
"start_time": "2025-01-18T15:54:49.694592Z"
}
},
"cell_type": "code",
"source": [
"calc_amount = partial(multiply, 5, 9)\n",
"print(calc_amount)"
],
"id": "36f412eba1f5c7a2",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"functools.partial(<function multiply at 0x1045a1620>, 5, 9)\n"
]
}
],
"execution_count": 9
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:54:59.034281Z",
"start_time": "2025-01-18T15:54:59.031514Z"
}
},
"cell_type": "code",
"source": "calc_amount()",
"id": "954d5fb6647a5008",
"outputs": [
{
"data": {
"text/plain": [
"45"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 10
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:55:41.487903Z",
"start_time": "2025-01-18T15:55:41.485680Z"
}
},
"cell_type": "code",
"source": [
"calc_amount7 = partial(multiply, 7, y=9)\n",
"print(calc_amount7)"
],
"id": "91d50b3631961887",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"functools.partial(<function multiply at 0x1045a1620>, 7, y=9)\n"
]
}
],
"execution_count": 11
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:55:50.567170Z",
"start_time": "2025-01-18T15:55:50.562470Z"
}
},
"cell_type": "code",
"source": "calc_amount7()",
"id": "83affc852a60b079",
"outputs": [
{
"data": {
"text/plain": [
"63"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 12
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:56:47.416590Z",
"start_time": "2025-01-18T15:56:47.414241Z"
}
},
"cell_type": "code",
"source": [
"def power(num, exponent):\n",
" return num**exponent"
],
"id": "3306667d79240b0e",
"outputs": [],
"execution_count": 13
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:56:51.030003Z",
"start_time": "2025-01-18T15:56:51.025379Z"
}
},
"cell_type": "code",
"source": "power(2, 3)",
"id": "5797414a5139eab3",
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 14
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:56:57.388375Z",
"start_time": "2025-01-18T15:56:57.385342Z"
}
},
"cell_type": "code",
"source": "power(3, 2)",
"id": "f1fdc98f6cf8e106",
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 15
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:57:18.472403Z",
"start_time": "2025-01-18T15:57:18.465383Z"
}
},
"cell_type": "code",
"source": "power(10, 2)",
"id": "9fab9396c459926c",
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 16
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:57:24.479266Z",
"start_time": "2025-01-18T15:57:24.476605Z"
}
},
"cell_type": "code",
"source": "power(2, 10)",
"id": "59f8905e0a9ee733",
"outputs": [
{
"data": {
"text/plain": [
"1024"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 17
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:57:52.978409Z",
"start_time": "2025-01-18T15:57:52.975770Z"
}
},
"cell_type": "code",
"source": "power(exponent=2, num=10)",
"id": "e2d36ec18c8d50ec",
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 18
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:58:49.049682Z",
"start_time": "2025-01-18T15:58:49.046487Z"
}
},
"cell_type": "code",
"source": [
"two_to_power = partial(power, 2)\n",
"print(two_to_power)"
],
"id": "26828044b3985b74",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"functools.partial(<function power at 0x1045a2d40>, 2)\n"
]
}
],
"execution_count": 19
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:58:58.640213Z",
"start_time": "2025-01-18T15:58:58.637966Z"
}
},
"cell_type": "code",
"source": "two_to_power(2)",
"id": "6e4275bb4083538a",
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 20
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:59:07.172132Z",
"start_time": "2025-01-18T15:59:07.169498Z"
}
},
"cell_type": "code",
"source": [
"print(two_to_power(3))\n",
"print(two_to_power(4))"
],
"id": "24e7a03de73a6e85",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"16\n"
]
}
],
"execution_count": 21
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:59:40.134031Z",
"start_time": "2025-01-18T15:59:40.131896Z"
}
},
"cell_type": "code",
"source": [
"square = partial(power, exponent=2)\n",
"print(square)"
],
"id": "d734c763b0b4aba",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"functools.partial(<function power at 0x1045a2d40>, exponent=2)\n"
]
}
],
"execution_count": 22
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T15:59:55.856755Z",
"start_time": "2025-01-18T15:59:55.854670Z"
}
},
"cell_type": "code",
"source": [
"print(square(2))\n",
"print(square(3))\n",
"print(square(4))"
],
"id": "352ce4e6c53708cf",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"9\n",
"16\n"
]
}
],
"execution_count": 23
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:00:15.479052Z",
"start_time": "2025-01-18T16:00:15.474065Z"
}
},
"cell_type": "code",
"source": "two_to_power(10)",
"id": "d9c0a6cab5a442b2",
"outputs": [
{
"data": {
"text/plain": [
"1024"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 24
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:00:32.408297Z",
"start_time": "2025-01-18T16:00:32.403404Z"
}
},
"cell_type": "code",
"source": "square(10)",
"id": "1bc746605637a879",
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 25
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:01:20.115679Z",
"start_time": "2025-01-18T16:01:20.112548Z"
}
},
"cell_type": "code",
"source": "square(num=3)",
"id": "e3f5c09f4bedc716",
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 26
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:03:09.653305Z",
"start_time": "2025-01-18T16:03:09.650426Z"
}
},
"cell_type": "code",
"source": [
"def send_email(to, subject, body):\n",
" print(f\"Sending email to {to}\")\n",
" print(f\"Subject: {subject}\")\n",
" print(f\"Body: {body}\")\n",
" print(\"-- Email sent --\")"
],
"id": "4ad899f862fd7b3",
"outputs": [],
"execution_count": 27
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:03:34.358326Z",
"start_time": "2025-01-18T16:03:34.356202Z"
}
},
"cell_type": "code",
"source": [
"send_email(\n",
" to=\"[email protected]\",\n",
" subject=\"Welcome Message\",\n",
" body=\"Hello Bob! Glad to see you're here!\"\n",
")"
],
"id": "8afa408f2ac3363f",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sending email to [email protected]\n",
"Subject: Welcome Message\n",
"Body: Hello Bob! Glad to see you're here!\n",
"-- Email sent --\n"
]
}
],
"execution_count": 28
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:04:42.236226Z",
"start_time": "2025-01-18T16:04:42.233591Z"
}
},
"cell_type": "code",
"source": [
"send_welcome = partial(\n",
" send_email,\n",
" subject=\"Welcome!\",\n",
")\n",
"print(send_welcome)"
],
"id": "9701cd55e6d66e0a",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"functools.partial(<function send_email at 0x1045a1b20>, subject='Welcome!')\n"
]
}
],
"execution_count": 29
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:05:04.821094Z",
"start_time": "2025-01-18T16:05:04.818982Z"
}
},
"cell_type": "code",
"source": [
"send_welcome(\n",
" \"[email protected]\",\n",
" body=\"Hello Bob! Glad to see you're here!\",\n",
")"
],
"id": "f7715bc057d8c9e3",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sending email to [email protected]\n",
"Subject: Welcome!\n",
"Body: Hello Bob! Glad to see you're here!\n",
"-- Email sent --\n"
]
}
],
"execution_count": 30
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:05:22.708727Z",
"start_time": "2025-01-18T16:05:22.706826Z"
}
},
"cell_type": "code",
"source": [
"send_welcome(\n",
" \"[email protected]\",\n",
" body=\"Hello John! Glad to see you're here!\",\n",
")"
],
"id": "842eb9ec55cc83e6",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sending email to [email protected]\n",
"Subject: Welcome!\n",
"Body: Hello John! Glad to see you're here!\n",
"-- Email sent --\n"
]
}
],
"execution_count": 31
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:05:47.581268Z",
"start_time": "2025-01-18T16:05:47.579318Z"
}
},
"cell_type": "code",
"source": [
"send_welcome(\n",
" to=\"[email protected]\",\n",
" body=\"Hello Alice! Glad to see you're here!\",\n",
")"
],
"id": "401f3b09c2806d25",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sending email to [email protected]\n",
"Subject: Welcome!\n",
"Body: Hello Alice! Glad to see you're here!\n",
"-- Email sent --\n"
]
}
],
"execution_count": 32
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:07:12.151498Z",
"start_time": "2025-01-18T16:07:12.149020Z"
}
},
"cell_type": "code",
"source": [
"send_generic_welcome = partial(\n",
" send_email,\n",
" subject=\"Welcome to our Site!\",\n",
" body=\"Hello dear user! We are glad to see you!\",\n",
")"
],
"id": "1fedd6736254e3fa",
"outputs": [],
"execution_count": 33
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:07:18.585060Z",
"start_time": "2025-01-18T16:07:18.582811Z"
}
},
"cell_type": "code",
"source": "send_generic_welcome(\"[email protected]\")",
"id": "436c0e166b38fb87",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sending email to [email protected]\n",
"Subject: Welcome to our Site!\n",
"Body: Hello dear user! We are glad to see you!\n",
"-- Email sent --\n"
]
}
],
"execution_count": 34
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-18T16:07:29.726085Z",
"start_time": "2025-01-18T16:07:29.724104Z"
}
},
"cell_type": "code",
"source": "send_generic_welcome(\"[email protected]\")",
"id": "c8349c7278361ff1",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sending email to [email protected]\n",
"Subject: Welcome to our Site!\n",
"Body: Hello dear user! We are glad to see you!\n",
"-- Email sent --\n"
]
}
],
"execution_count": 35
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
anyio==4.8.0
appnope==0.1.4
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asttokens==3.0.0
async-lru==2.0.4
attrs==24.3.0
babel==2.16.0
beautifulsoup4==4.12.3
bleach==6.2.0
certifi==2024.12.14
cffi==1.17.1
charset-normalizer==3.4.1
comm==0.2.2
debugpy==1.8.12
decorator==5.1.1
defusedxml==0.7.1
executing==2.1.0
fastjsonschema==2.21.1
fqdn==1.5.1
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
ipykernel==6.29.5
ipython==8.31.0
isoduration==20.11.0
jedi==0.19.2
Jinja2==3.1.5
json5==0.10.0
jsonpointer==3.0.0
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
jupyter-events==0.11.0
jupyter-lsp==2.2.5
jupyter_client==8.6.3
jupyter_core==5.7.2
jupyter_server==2.15.0
jupyter_server_terminals==0.5.3
jupyterlab==4.3.4
jupyterlab_pygments==0.3.0
jupyterlab_server==2.27.3
MarkupSafe==3.0.2
matplotlib-inline==0.1.7
mistune==3.1.0
nbclient==0.10.2
nbconvert==7.16.5
nbformat==5.10.4
nest-asyncio==1.6.0
notebook==7.3.2
notebook_shim==0.2.4
overrides==7.7.0
packaging==24.2
pandocfilters==1.5.1
parso==0.8.4
pexpect==4.9.0
platformdirs==4.3.6
prometheus_client==0.21.1
prompt_toolkit==3.0.48
psutil==6.1.1
ptyprocess==0.7.0
pure_eval==0.2.3
pycparser==2.22
Pygments==2.19.1
python-dateutil==2.9.0.post0
python-json-logger==3.2.1
PyYAML==6.0.2
pyzmq==26.2.0
referencing==0.36.1
requests==2.32.3
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rpds-py==0.22.3
Send2Trash==1.8.3
setuptools==75.8.0
six==1.17.0
sniffio==1.3.1
soupsieve==2.6
stack-data==0.6.3
terminado==0.18.1
tinycss2==1.4.0
tornado==6.4.2
traitlets==5.14.3
types-python-dateutil==2.9.0.20241206
typing_extensions==4.12.2
uri-template==1.3.0
urllib3==2.3.0
wcwidth==0.2.13
webcolors==24.11.1
webencodings==0.5.1
websocket-client==1.8.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment