【Python3認定基礎試験】関数における引数のデフォルト値について解説【Pythonチュートリアル(4章)】

カテゴリ:Python 投稿:2022年7月24日2:42、更新:2022年8月17日0:03

・記事の概要 

Pythonチュートリアル(第4版)の4章内にある「関数における引数のデフォルト値」について

初学者や非エンジニアの方にも、なるべく分かりやすくまとめます!


・この記事の対象 

Python認定基礎試験を受験する方(非エンジニアの方含み)

・Pythonチュートリアルの読者で内容が腹落ちしていない方

・引数のデフォルト値で可変(ミュータブル)か否かが、どう関係するのか気になった方


・なんの解説か 

Pythonチュートリアル(第4版)を章ごとに解説していきます。

今回はPythonチュートリアル30ページ目からの記載になりますが

・引数のデフォルト値は一度しか評価されない

・デフォルト値が可変オブジェクトの場合はこれが影響する

とあります。

おそらく初見だと「ん??」となるかもなので、ここを例を交えて解説します。


書籍の準備がまだという方は、購入をお勧めします。

内容が分かりにくいというレビューも聞きますが、

ベースをかなりしっかり抑えた内容ですので、

資格取得という面ではなくとも手元にあるといいものです。



・デフォルト値は一度しか評価されない、とは 

実際のコードも交えて解説します。

前提で言えば、ここの引数Lはリストであり、可変オブジェクトに該当します。

そのため、デフォルト値が一度しか評価されないことの影響を受けると言えますね。

説明コード1

ほとんど画像内で説明してしまっていますね 笑

リストは可変オブジェクトですので、デフォルト値が毎回評価されれば上記の通り

[1] [2] [3] がそれぞれ出力結果になるのです。


ただデフォルト値は一度だけしか評価されないので2回目以降も

それ以前のappendされたリストに追加されていくことになります。

それでは可変か不変かについてはどうでしょう。


・可変オブジェクトの場合影響する、とは 

こちらもほとんど画像内で説明しておりますが

リストが可変(ミュータブル)なのに対し、stringやintは不変(イミュータブル)となります。

※ミュータブル/イミュータブルの説明自体はここでは割愛しますが、

「変数に新たに値を代入できる=ミュータブル」というわけではないのでご留意ください。


以下の例ではbをintとしており、かつ引数でデフォルト値設定もしています。


説明コード2

ポイントとしては関数内で最後に「b=5」としていることです。

ただ出力結果を見ると、2回目の実行でも

「b=2」のまま処理されていることがわかります。

これは「b=5」はあくまで関数内の変数として再定義されているからです。


そして、引数に不変体を設定した際には「関数内での変数の変更=上記の再定義」となります。

なのでデフォルト値が変わらない=「影響を受けない」と言えるのです。


逆に言ってしまえば、

引数が可変体で関数の中で要素の変更をした場合は、変更は有効です。

それは最初の例でリストにappendしたことも同様ですので、再度確認してみてください。



・まとめ 

いかがだったでしょうか。

Python認定基礎試験の解説記事は今後も続けてみようと思いますが、

皆様の参考になれば幸いです。


「本当にプログラミング自体が初心者で」という場合は公式書籍の前に

「スッキリシリーズ」のテキストから入ると挿絵や説明がかなり分かりやすく

初めての理解には役立つかもしれません。

私も未経験の新卒時にはJavaで同じシリーズを読んでいました。

(経験者からすると少し冗長的かもしれないが、それくらい丁寧である)



・関連記事 

Python3認定基礎試験関連の記事が以下にもあります。

参考までにぜひ!


第9章を読み始める際に知っておいてほしい解説記事

初学者にクラスについて最初に抑えておいてもらいたいこと


第9章の「変数の名前空間管理」について図解した記事

図とソースで初学者に変数のスコープを解説


・1問1答形式で使いやすい演習問題サイトの紹介記事

Python3エンジニア認定基礎試験の使いやすい演習問題サイトをご紹介!


・模試や試験前の確認チートシート

コメント一覧

名無し / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:42
123456
expr 999989283 + 800956259

名無し / 2023年9月13日7:42
123456

名無し / 2023年9月13日7:43
123456|expr 860863386 + 901512322

名無し / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456$(expr 908665530 + 823178315)

名無し / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456

${@var_dump(md5(463760439))}; / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456&set /A 892036010+811391687

名無し / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456

${948210822+841959194} / 2023年9月13日7:43
123456

'-var_dump(md5(399964059))-' / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
expr 883189278 + 988602153

名無し / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
${894282712+963131338}

名無し / 2023年9月13日7:43
${@var_dump(md5(165239020))};

名無し / 2023年9月13日7:43
'-var_dump(md5(806408198))-'

名無し / 2023年9月13日7:43
123456

/*1*/{{853031744+895948572}} / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456

${992965882+875124851} / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
123456

${(963466546+844701772)?c} / 2023年9月13日7:43
123456

名無し expr 856219336 + 938686691 / 2023年9月13日7:43
123456

<%- 809272817+942100857 %> / 2023年9月13日7:43
123456

名無し|expr 988931598 + 814460223 / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
/*1*/{{975900968+969358248}}

名無し / 2023年9月13日7:43
${941150892+955478919}

名無し&set /A 977558243+919850098 / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
${(818482843+823172350)?c}

expr 824375825 + 972523634 / 2023年9月13日7:43
123456

名無し / 2023年9月13日7:43
#set($c=908413320+925165404)${c}$c

名無し / 2023年9月13日7:43
<%- 860790038+903523285 %>

名無し'and'z'='z / 2023年9月13日7:43
123456

名無し鎈'"\( / 2023年9月13日7:43
123456

名無し'and't'='r / 2023年9月13日7:43
123456

名無し'"\( / 2023年9月13日7:43
123456

名無し"and"r"="r / 2023年9月13日7:44
123456

名無し / 2023年9月13日7:44
123456'and/**/extractvalue(1,concat(char(126),md5(1605808536)))and'

名無し"and"l"="a / 2023年9月13日7:44
123456

名無し / 2023年9月13日7:44
123456"and/**/extractvalue(1,concat(char(126),md5(1417963311)))and"

名無し / 2023年9月13日7:44
extractvalue(1,concat(char(126),md5(1531744001)))

名無し / 2023年9月13日7:44
123456'and(select'1'from/**/cast(md5(1051604371)as/**/int))>'0

名無し / 2023年9月13日7:44
123456/**/and/**/cast(md5('1386709204')as/**/int)>0

名無し / 2023年9月13日7:44
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1021952723')))

名無し / 2023年9月13日7:44
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1158768575')))>'0

名無し / 2023年9月13日7:44
123456鎈'"\(

名無し / 2023年9月13日7:44
123456'"\(

名無し / 2023年9月13日7:44
123456/**/and+1=1

名無し / 2023年9月13日7:44
123456/**/and+2=9

名無し / 2023年9月13日7:44
123456'and'e'='e

名無し / 2023年9月13日7:44
123456'and'e'='y

名無し / 2023年9月13日7:44
123456"and"n"="n

名無し / 2023年9月13日7:44
123456"and"r"="n

名無し / 2023年9月13日7:44
(select*from(select+sleep(0)union/**/select+1)a)

名無し / 2023年9月13日7:45
(select*from(select+sleep(2)union/**/select+1)a)

名無し / 2023年9月13日7:45
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='

名無し / 2023年9月13日7:45
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='

名無し / 2023年9月13日7:45
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="

名無し / 2023年9月13日7:45
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="

名無し / 2023年9月13日7:45
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/

名無し / 2023年9月13日7:45
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/

名無し / 2023年9月13日7:45
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0

名無し / 2023年9月13日7:45
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0

名無し / 2023年9月13日7:45
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/

名無し / 2023年9月13日7:45
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/

名無し / 2023年9月13日7:45
123456'and(select+1)>0waitfor/**/delay'0:0:0

名無し / 2023年9月13日7:45
123456'and(select+1)>0waitfor/**/delay'0:0:2

名無し / 2023年9月13日7:45
123456/**/and/**/2=DBMS_PIPE.RECEIVE_MESSAGE('r',0)

名無し / 2023年9月13日7:45
123456/**/and/**/1=DBMS_PIPE.RECEIVE_MESSAGE('x',2)

名無し / 2023年9月13日7:45
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('r',0)='r

名無し / 2023年9月13日7:45
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('w',2)='w

名無し / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:30
123456

名無し expr 846019640 + 802946107 / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
${@var_dump(md5(104694054))};

名無し|expr 813901709 + 968145211 / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
'-var_dump(md5(748573187))-'

名無し / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456

名無し&set /A 828447923+901958628 / 2023年11月7日15:32
123456

${853403596+911207449} / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
123456/**/and+2=2

名無し / 2023年11月7日15:32
123456

expr 890715230 + 931968513 / 2023年11月7日15:32
123456

名無し / 2023年11月7日15:32
${922864120+958247137}

名無し / 2023年11月7日15:33
123456/**/and+2=5

${@var_dump(md5(676558571))}; / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
123456
expr 904368141 + 844841548

名無し / 2023年11月7日15:33
123456'and'm'='m

'-var_dump(md5(384002202))-' / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
123456|expr 977545505 + 820116439

/*1*/{{930141404+907853807}} / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
123456'and'x'='g

名無し / 2023年11月7日15:33
123456$(expr 814081211 + 968021346)

${988026046+975911377} / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
123456"and"s"="s

名無し / 2023年11月7日15:33
123456&set /A 850734501+880083432

${(893268167+811572047)?c} / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
123456"and"m"="i

名無し / 2023年11月7日15:33
expr 987668645 + 878612023

名無し / 2023年11月7日15:33
123456

<%- 830038811+994368548 %> / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
/*1*/{{877174056+883751722}}

名無し / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
${965711761+895987656}

名無し / 2023年11月7日15:33
(select*from(select+sleep(0)union/**/select+1)a)

名無し / 2023年11月7日15:33
${(895223563+973756463)?c}

名無し / 2023年11月7日15:33
(select*from(select+sleep(2)union/**/select+1)a)

名無し / 2023年11月7日15:33
#set($c=833910499+956763656)${c}$c

名無し / 2023年11月7日15:33
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='

名無し / 2023年11月7日15:33
<%- 820183059+851585727 %>

名無し / 2023年11月7日15:33
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='

名無し / 2023年11月7日15:33
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="

名無し / 2023年11月7日15:33
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="

名無し / 2023年11月7日15:33
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/

名無し鎈'"\( / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:33
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/

名無し'"\( / 2023年11月7日15:33
123456

名無し / 2023年11月7日15:34
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0

名無し / 2023年11月7日15:34
123456'and/**/extractvalue(1,concat(char(126),md5(1926088044)))and'

名無し / 2023年11月7日15:34
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0

名無し / 2023年11月7日15:34
123456"and/**/extractvalue(1,concat(char(126),md5(1792542168)))and"

名無し / 2023年11月7日15:34
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/

名無し / 2023年11月7日15:34
extractvalue(1,concat(char(126),md5(1291436731)))

名無し / 2023年11月7日15:34
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/

名無し / 2023年11月7日15:34
123456'and(select'1'from/**/cast(md5(1541465040)as/**/int))>'0

名無し / 2023年11月7日15:34
123456'and(select+1)>0waitfor/**/delay'0:0:0

名無し / 2023年11月7日15:34
123456/**/and/**/cast(md5('1628512296')as/**/int)>0

名無し / 2023年11月7日15:34
123456'and(select+1)>0waitfor/**/delay'0:0:2

名無し / 2023年11月7日15:34
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1917877346')))

名無し / 2023年11月7日15:34
123456/**/and/**/3=DBMS_PIPE.RECEIVE_MESSAGE('r',0)

名無し / 2023年11月7日15:34
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1791665926')))>'0

名無し / 2023年11月7日15:34
123456/**/and/**/1=DBMS_PIPE.RECEIVE_MESSAGE('g',2)

名無し / 2023年11月7日15:34
123456鎈'"\(

名無し / 2023年11月7日15:34
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('c',0)='c

名無し / 2023年11月7日15:34
123456'"\(

名無し / 2023年11月7日15:34
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('x',2)='x

名無し'and'y'='y / 2023年11月7日15:35
123456

名無し'and'i'='l / 2023年11月7日15:35
123456

名無し"and"e"="e / 2023年11月7日15:35
123456

名無し"and"r"="s / 2023年11月7日15:35
123456

コメント投稿ページへ


書いている人

のぎ
PG->SE->PMとして働き、現在はIT企業で新卒採用の担当をしています。長期育休取得明けで0歳娘の対応に毎日バタバタです。笑
採用業務をしつつ社内利用ツールなど作成しており、当ブログも勉強の一環でコーディングして作成しています。 エンジニア領域、人事・採用領域、育児関連など発信していきます。