【Python3認定基礎試験】初学者にクラスについて最初に抑えておいてもらいたいこと【Pythonチュートリアル(9章)】

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

・記事の概要 

Pythonチュートリアル(第4版)の9章で「クラス」に関する説明が続きますが

内容を読む前に知っておいてほしい基礎を説明します。

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


・この記事の対象 

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

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

・クラスの章の内容がまだ不明瞭な方、これから読む方


・なんの解説か 

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


今回はPythonチュートリアルの9章についての記載ですが

97ページ章冒頭1行目に

「クラスとはデータと機能をひとまとめにする方法を提供するものだ」

とあります。


初見だと「ふーん」と思いながら読み進めてしまうかもしれません。

ただ、これクラスの「キホンのキ」に当たる大事な部分です。

書籍だと知っていて当たり前なテイストですが、初学者には是非抑えてほしい。


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

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

(※実際に初学者へ凄く丁寧な説明というわけではないが)

要点をかなりしっかり抑えた内容ですので、

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



・データと機能をひとまとめにする、とは 

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

「クラスとはデータと機能をひとまとめにする方法を提供するものだ」

を理解するためにまずは関数(def)をみてみましょう。


関数のコード


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

関数(def)には引数を渡し、それを元に処理(ここでは計算)をするだけで

値(データ)の保持はしていません。

もちろん、関数内に変数を定義することも可能ですが

(例えば area = height*base/2 とし、return area のような)

それは保持というよりも処理終了までの「暫定的な入れ物」という意味合いが強いでしょう。


ではクラスではどうでしょうか。

クラスのコード


クラスがインスタンス生成( area_cal() )される度に、

オブジェクトごとに値が初期化され、

オブジェクトごとに値を保持することが可能になっています。

これが「クラスとはデータと機能をひとまとめにする」という状態です。


・つまりどういうことか

上記を簡単にまとめると以下のようになります。


・関数  

高さ・底辺・求められる面積といった値(データ)を関数内に保持していない。

・クラス  

高さ・底辺・求められる面積といった値(データ)をオブジェクトごとにクラス内で保持でき

その上で関数(データを扱う処理)を内部に持てる。

インスタンスオブジェクトごとにそれぞれ値を保持できる。

またこの例だと、

やっていることも小規模でクラス利用の方がコードも長いしクラスって必要なの?

と思うかもしれませんが、

複数人で開発する際や、継承を使うことを考えると必須となります。

(逆に個人かつ小規模なものであれば不要なことも多い)

あくまで説明用の例ですので、その辺はご容赦ください。



・まとめ 

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

今回の内容は非常に基本的ですが、

ここがわかっていないとクラスの章を読み進めるのは

結構しんどいと思いますので解説しました。


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

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


繰り返しにはなりますが、受験には公式書籍があった方がいいですので

メインは書籍でインプットをしていきましょう!



Pythonチュートリアル 第4版


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

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

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

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

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



・関連記事 

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

参考までにぜひ!


・第4章(P30~)に関する説明記事

関数における引数のデフォルト値について解説


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

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


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

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


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

コメント一覧

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

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

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

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

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

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

${854660517+823362901} / 2023年9月13日7:38
123456

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

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

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

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

名無し / 2023年9月13日7:38
${908269046+864008188}

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

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

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

名無し expr 888079440 + 995074154 / 2023年9月13日7:38
123456

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

名無し|expr 824153076 + 861479377 / 2023年9月13日7:38
123456

/*1*/{{910705474+944918284}} / 2023年9月13日7:38
123456

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

${983523665+810898174} / 2023年9月13日7:38
123456

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

名無し&set /A 971701951+997075892 / 2023年9月13日7:38
123456

${(897597301+913165752)?c} / 2023年9月13日7:38
123456

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

expr 944488656 + 971707448 / 2023年9月13日7:38
123456

名無し / 2023年9月13日7:38
123456
expr 986045560 + 847259886

<%- 938693979+831985961 %> / 2023年9月13日7:38
123456

名無し / 2023年9月13日7:38
123456|expr 964039758 + 827083902

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

名無し / 2023年9月13日7:38
/*1*/{{861258215+828810081}}

名無し / 2023年9月13日7:38
123456$(expr 860120197 + 807165900)

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

名無し / 2023年9月13日7:38
${973196730+919181699}

名無し / 2023年9月13日7:38
123456&set /A 991257070+855098697

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

名無し / 2023年9月13日7:38
${(826373223+877961717)?c}

名無し / 2023年9月13日7:38
expr 964780660 + 843321073

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

名無し / 2023年9月13日7:38
#set($c=999044275+929054214)${c}$c

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

名無し / 2023年9月13日7:38
<%- 850581618+928176460 %>

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

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

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

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

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

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

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

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

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

名無し"and"h"="e / 2023年9月13日7:39
123456

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

名無し / 2023年9月13日7:39
123456/**/and+0=8

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

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

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

名無し / 2023年9月13日7:40
123456"and"m"="b

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

名無し / 2023年11月7日15:34
${866309008+901288410}

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

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

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

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

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

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

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

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

${879136935+857482751} / 2023年11月7日15:34
123456

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

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

名無し expr 918849266 + 948499991 / 2023年11月7日15:34
123456

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

名無し|expr 889984493 + 883690869 / 2023年11月7日15:34
123456

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

/*1*/{{858630261+887109564}} / 2023年11月7日15:34
123456

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

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

${937370821+862654274} / 2023年11月7日15:34
123456

名無し&set /A 991507453+818187829 / 2023年11月7日15:34
123456

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

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

${(843751732+859297143)?c} / 2023年11月7日15:34
123456

expr 846740472 + 930068312 / 2023年11月7日15:34
123456

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

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

名無し / 2023年11月7日15:34
123456
expr 932057988 + 851578022

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

名無し / 2023年11月7日15:34
123456|expr 811948579 + 817400475

<%- 924849893+828458527 %> / 2023年11月7日15:34
123456

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

名無し / 2023年11月7日15:34
123456$(expr 962144237 + 997922680)

名無し / 2023年11月7日15:34
/*1*/{{830810870+930994002}}

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

名無し / 2023年11月7日15:34
123456&set /A 936136755+992854522

名無し / 2023年11月7日15:34
${965951354+973436599}

名無し / 2023年11月7日15:35
expr 905730318 + 840793839

名無し / 2023年11月7日15:35
${(884361647+862940819)?c}

名無し / 2023年11月7日15:35
#set($c=924939905+969909889)${c}$c

名無し / 2023年11月7日15:35
<%- 897446835+873015034 %>

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

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

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

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

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

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

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

名無し / 2023年11月7日15:36
123456/**/and+3=6

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

コメント投稿ページへ


書いている人

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