summaryrefslogtreecommitdiff
path: root/basics
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-08-21 09:41:53 +0800
committerzhang <zch921005@126.com>2022-08-21 09:41:53 +0800
commit94b6d3246c72eb3cae58a2fd18771e3c2c3e7cb2 (patch)
treee23a287289cdca8020fab062cdebffa60b021d7f /basics
parent756b736ca374dc6ef2adadce101f380e10f06c4e (diff)
copy
Diffstat (limited to 'basics')
-rw-r--r--basics/python/default_mutable_parameters.ipynb428
1 files changed, 428 insertions, 0 deletions
diff --git a/basics/python/default_mutable_parameters.ipynb b/basics/python/default_mutable_parameters.ipynb
new file mode 100644
index 0000000..c133f86
--- /dev/null
+++ b/basics/python/default_mutable_parameters.ipynb
@@ -0,0 +1,428 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 1. case 1"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:27:26.778739Z",
+ "start_time": "2022-08-21T01:27:26.765873Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "def add_employee(emp, emp_list = []):\n",
+ " emp_list.append(emp)\n",
+ " print(emp_list)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:27:36.118235Z",
+ "start_time": "2022-08-21T01:27:36.113245Z"
+ },
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "['zhang']\n"
+ ]
+ }
+ ],
+ "source": [
+ "add_employee('zhang')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:27:39.612702Z",
+ "start_time": "2022-08-21T01:27:39.601724Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "['zhang', 'li']\n"
+ ]
+ }
+ ],
+ "source": [
+ "add_employee('li')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:28:01.433747Z",
+ "start_time": "2022-08-21T01:28:01.430249Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "['li']\n"
+ ]
+ }
+ ],
+ "source": [
+ "add_employee('li', [])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:28:15.988491Z",
+ "start_time": "2022-08-21T01:28:15.984261Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "['zhang', 'li', 'li']\n"
+ ]
+ }
+ ],
+ "source": [
+ "add_employee('li')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:30:02.765148Z",
+ "start_time": "2022-08-21T01:30:02.744801Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(['zhang', 'li', 'li'],)"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "add_employee.__defaults__"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:30:44.627317Z",
+ "start_time": "2022-08-21T01:30:44.623666Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "def add_employee(emp, emp_list = []):\n",
+ " print(add_employee.__defaults__)\n",
+ " emp_list.append(emp)\n",
+ " print(emp_list)\n",
+ " print(add_employee.__defaults__)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:30:46.009755Z",
+ "start_time": "2022-08-21T01:30:46.002688Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "([],)\n",
+ "['zhang']\n",
+ "(['zhang'],)\n"
+ ]
+ }
+ ],
+ "source": [
+ "add_employee('zhang')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:31:09.233929Z",
+ "start_time": "2022-08-21T01:31:09.230161Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(['zhang'],)\n",
+ "['zhang', 'li']\n",
+ "(['zhang', 'li'],)\n"
+ ]
+ }
+ ],
+ "source": [
+ "add_employee('li')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:31:45.175683Z",
+ "start_time": "2022-08-21T01:31:45.172977Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(['zhang', 'li'],)\n",
+ "['li']\n",
+ "(['zhang', 'li'],)\n"
+ ]
+ }
+ ],
+ "source": [
+ "add_employee('li', [])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:33:18.260279Z",
+ "start_time": "2022-08-21T01:33:18.254488Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "def add_employee_fixed(emp, emp_list = None):\n",
+ " if emp_list is None:\n",
+ " emp_list = []\n",
+ " emp_list.append(emp)\n",
+ " print(emp_list)\n",
+ " print(add_employee_fixed.__defaults__)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:33:31.485064Z",
+ "start_time": "2022-08-21T01:33:31.481392Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(None,)\n",
+ "['zhang']\n",
+ "(None,)\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(add_employee_fixed.__defaults__)\n",
+ "add_employee_fixed('zhang')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 2. case 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:28:52.580680Z",
+ "start_time": "2022-08-21T01:28:52.576724Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "from datetime import datetime\n",
+ "import time"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:34:32.671514Z",
+ "start_time": "2022-08-21T01:34:32.668551Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\n",
+ "def print_time(time_to_print=datetime.now()):\n",
+ " print(time_to_print.strftime('%b %d, %Y %H:%M:%S'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:34:48.345468Z",
+ "start_time": "2022-08-21T01:34:48.341096Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(datetime.datetime(2022, 8, 21, 9, 34, 32, 669555),)"
+ ]
+ },
+ "execution_count": 23,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "print_time.__defaults__"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:35:35.002020Z",
+ "start_time": "2022-08-21T01:35:34.998605Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\n",
+ "def print_time_fixed(time_to_print=None):\n",
+ " if time_to_print is None:\n",
+ " time_to_print = datetime.now()\n",
+ " print(time_to_print.strftime('%b %d, %Y %H:%M:%S'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:35:39.491909Z",
+ "start_time": "2022-08-21T01:35:39.488710Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Aug 21, 2022 09:35:39\n"
+ ]
+ }
+ ],
+ "source": [
+ "print_time_fixed()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2022-08-21T01:35:43.785204Z",
+ "start_time": "2022-08-21T01:35:43.782140Z"
+ }
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Aug 21, 2022 09:35:43\n"
+ ]
+ }
+ ],
+ "source": [
+ "print_time_fixed()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "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.6.8"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}