WTF Python!ð± ïŒPythonããŸãããïŒïŒåç·š
以äžã®æç« ã¯ãã¡ãã®ããŒãžã翻蚳ãããã®ã§ãã
https://github.com/satwikkansal/wtfpython
ãã¡ãã®GitHubãããžã§ã¯ããWhat the f*ck Python! ð±ãã¯ãäºæ³å€ã®ã³ãŒãã¹ãããããéããŠPythonãæ¢æ±ããç解ããããšãç®æããŠããŸããå€ãã®èšèªã«ç¿»èš³ãããŠããããŠã§ããµã€ããããŒãããã¯ãã³ãã³ãã©ã€ã³ããŒã«ãéããŠã€ã³ã¿ã©ã¯ãã£ãã«åŠã¶ããšãã§ããŸãã
ä»ã®ã¢ãŒã: Interactive Website | Interactive Notebook | CLI
Pythonã¯é«æ°Žæºãªè§£éåã®ããã°ã©ãã³ã°èšèªã§ãããéçºè ãå¿«é©ã«å©çšããããã«å€ãã®æ©èœãæèŒããŠããŸããããããäžèŠããã ãã§ã¯ç解ãé£ãPythonã¹ããããã®çµæãšããã®ããããŸãã
ãã¡ãã®ã¯ã¯ã¯ã¯ãããããžã§ã¯ãã§ã¯ãçŽæã«åããã¹ãããããPythonã®ããŸãç¥ãããŠããªãç¹åŸŽã®èåŸã«ããæ£ç¢ºãªã¡ã«ããºã ã®è§£èª¬ãè©Šã¿ãŸãããããŸã§é©ããã®ã§ã¯ãªããããªäŸãåãäžããããŠãããããããŸããããPythonã®é¢çœããšãããç解ããããããŠãããŸãããã®ãããžã§ã¯ãã¯ããã°ã©ãã³ã°èšèªã®å¥¥æ·±ããåŠã¶è¯ãæ段ã§ããèªè ã§ããããªãã«ãšã£ãŠãèå³æ·±ããã®ã«ãªãã§ãããã
çµéšè±å¯ãªPythonããã°ã©ããŒã®æ¹ã¯ãäžçºã§æ£è§£ãåãããããè©Šããã ãããããã€ãã®äºè±¡ã¯ä»¥åã«çµéšãããŠãããããããŸãããå€ãèšæ¶ãåŒã³æ»ãããšãã§ããã§ããããïŒ
P.S. ãã®èšäºãèªãã®ãäºåºŠç®ä»¥éã ãšããæ¹ã¯ãææ°çã€ããŠã¯ãã¡ããã芧ãã ããïŒææ°ã®æ¹èšã§è¿œå ãããäŸã«ã¯ã¢ã¹ã¿ãªã¹ã¯ãä»ããŠããŸãïŒã
ããã§ã¯ãå§ããŸããã...
ç®æ¬¡
- äŸã®æ§é
- 䜿ãæ¹
- å®äŸ
- ã»ã¯ã·ã§ã³: è³ãã¬ãã£ã¬ã³ãžïŒ
- ãŸãã¯å€§äºãªããšããïŒ
- ãã£ãããªæåå
- é£éããæäœã«ã¯æ³šæãå¿ èŠ
- **
is
**æŒç®åã®èœãšãç©Ž - ããã·ã¥ãã©ãŠããŒ
- æ¬è³ªçã«ã¯ã¿ããªåã
- 秩åºã®äžã®ç¡ç§©åº
- ãã©ã€ãç¶ãããïŒ
- äœã®ãããªã®ã
- è©äŸ¡æéã®äžäžèŽ
is not
ã¯is not
ã§ã¯ãªã- äžçºã§Xãåã€äžç®äžŠã¹ïŒ
- ã·ã¥ã¬ãŒãã£ã³ã¬ãŒã®å€æ°
- é¶ãå ãåµãå ã
- ãµãã¯ã©ã¹ã®ãªã¬ãŒã·ã§ã³
- ã¡ãœããã®ç䟡æ§ãšåäžæ§
- ãã¹ãŠçã§ãã
- é©ãã®ã³ã³ã
- æååãšããã¯ã¹ã©ãã·ã¥
not
ã®çµã³ç®- ååã®ããªãã«ã¯ã©ãŒãæåå
- ããŒã«å€ã®æå€ãªæå
- ã¯ã©ã¹å±æ§ãšã€ã³ã¹ã¿ã³ã¹å±æ§ã®éã
- Noneã®çæ
return
ããyield
- Nanååž°æ§
- äžå€ã®å€ç°
- å€éšã¹ã³ãŒãããæ¶ããå€æ°
- è¬ã®ããŒåå€æ
- ããã¯åããããªïŒ
- æŽæ°ã®æååå€æãéçãè¶ ãããšã
- ã»ã¯ã·ã§ã³: æ»ããããæé¢
- ã€ãã¬ãŒãäžã®èŸæžå€æŽåé¡
- ãã€ãã
del
æäœ - ã¹ã³ãŒãå€ã®å€æ°
- ã€ãã¬ãŒãäžã®ãªã¹ãé ç®åé€
- ã€ãã¬ãŒã¿ã®ããã·ãŒå§çž®
- ã«ãŒãå€æ°ãæŒããïŒ
- ããã©ã«ãã®å¯å€åŒæ°ã«æ³šæïŒ
- äŸå€åŠçã®èœãšãç©Ž
- åããªãã©ã³ããç°ãªãçµæïŒ
- ã¯ã©ã¹ã¹ã³ãŒããç¡èŠããåå解決
- ãéè¡å®¶ã®äžžãã
- èã®äžã®é
- Splitsies
- ã¯ã€ã«ãã€ã³ããŒã
- ãã¹ãŠsortedïŒ
- çå€äžã¯ååšããªãïŒ
- ã»ã¯ã·ã§ã³: é ãããå®ç©ãã¡ïŒ
- Pythonã空ãé£ã³ãããã ãã©âŠ
- ãªã
goto
ãªã®ã - ã«ãã³ã€ãããïŒ
- barryãããã
- Pythonã§ãç¥ã£ãŠããããšïŒæã¯è€é
- å®ã¯ååšãããã§ãïŒ
- Ellipsis
- ã€ã³ãããã£
- ãã³ã°ãªã³ã°ããã
- ã»ã¯ã·ã§ã³: èŠãç®ã«æãããããª
- è¡ãã¹ãããïŒ
- ãã¬ããŒãããŒã·ã§ã³
- äœãããããã...
- ã»ã¯ã·ã§ã³: ããŸããŸãªãããã¯
+=
ã®æ¹ãæ©ã- 巚倧ãªæååã®äœæ
dict
æ€çŽ¢ã®é 延- ã€ã³ã¹ã¿ã³ã¹
dict
ã®èšåŒµ - ãã®ä»ã®å°ããªçºèŠ
- ã»ã¯ã·ã§ã³: è³ãã¬ãã£ã¬ã³ãžïŒ
äŸã®æ§é
å šãŠã®äŸã¯ä»¥äžã®ãããªæ§é ã§è¡šç€ºãããŠããŸã:
ⶠé¢çœãã¿ã€ãã«
#ã³ãŒã
#é¢çœãããšãèµ·ããŸãã...
åºåïŒPythonã®ããŒãžã§ã³ïŒ
>>> ããªã¬ãŒãšãªãæ
äœããã®äºæž¬ã§ããªãåºå
ïŒå Žåã«ãã£ãŠã¯ïŒããã§äžèš
ð¡ 解説:
äœãèµ·ããŠããã®ãããªãèµ·ããŠããã®ãã®ç°¡åãªèª¬æã
#ã³ãŒã
#å¿
èŠããã°ã解説ã®ããã«äŸãè¿œå
åºåïŒPythonã®ããŒãžã§ã³ïŒ
>>> ããªã¬ãŒ # äœãèµ·ããŠããã®ããç°¡åã«ç解ããããã®äŸ
#ãæ£ããåºå
泚æïŒå šãŠã®äŸã¯Python 3.5.2ã®å¯Ÿè©±åã€ã³ã¿ãŒããªã¿ã§ãã¹ããããŠãããåºåã®åã«ç¹ã«æå®ããªãéããå šãŠã®PythonããŒãžã§ã³ã§åäœããã¯ãã§ãã
䜿ãæ¹
äœè ã®èãããããããã®äŸãæ倧éã«æŽ»çšããããã®æ¹æ³ã§ãã
-
äŸã®æåã®ã³ãŒãã泚ææ·±ãèªãã§ãã ãããçµéšè±å¯ãªPythonããã°ã©ããŒã§ããã°ãã»ãšãã©ã®å Žåã次ã«äœãèµ·ããããäºæž¬ã§ããã§ãããã
-
åºåã¹ãããããèªã¿ã
- ããªããæåŸ ãããã®ãšåããã©ãããã確èªãã ããã
- ãã®ãããªç¶æ
ã«ãªã£ãŠããçç±ãæ£ç¢ºã«ç解ã§ããŠããŸããïŒ
- çãããããããã§ããã°ïŒå šãåé¡ãããŸããïŒãæ·±åŒåžãããŠã解説ãèªãã§ãã ããã
- çãããã¯ããã§ããã°ãèªåã®ããšãè€ããäžã§ã次ã®äŸã«é²ãã§ãã ããã
P.S.ïŒã³ãã³ãã©ã€ã³ã䜿çšããŠWTFPythonãèªãããšãã§ããŸããpypi packageã䜿çšããŠã
$ pip install wtfpython -U $ wtfpython
ð äŸ
ã»ã¯ã·ã§ã³: è³ãã¬ãã£ã¬ã³ãžïŒ
ⶠãŸãã¯å€§äºãªããšããïŒ
Python 3.8ã®ãã»ã€ãŠããæŒç®åïŒ:=
ïŒããªãã人æ°ãåããŠããŸããããã«ã€ããŠèŠãŠã¿ãŸãããã
# PythonããŒãžã§ã³3.8以é
>>> a = "wtf_walrus"
>>> a
'wtf_walrus'
>>> a := "wtf_walrus"
File "<stdin>", line 1
a := "wtf_walrus"
^
SyntaxError: invalid syntax
>>> (a := "wtf_walrus") # ããã¯åäœããŸã
'wtf_walrus'
>>> a
'wtf_walrus'
# PythonããŒãžã§ã³3.8以é
>>> a = 6, 9>>> a
(6, 9)
>>> (a := 6, 9)
(6, 9)
>>> a
6
>>> a, b = 6, 9 # éåžžã®ã¢ã³ããã¯>>> a, b
(6, 9)
>>> (a, b = 16, 19) # ãã£ãš
File "<stdin>", line 1
(a, b = 16, 19)
^
SyntaxError: invalid syntax
>>> (a, b := 16, 19) # å€ãª3ã€çµãåºåãããŸã
(6, 16, 19)
>>> a # aã¯ãŸã å€ãã£ãŠããŸãã
6
>>> b
16
ð¡ 解説
ã»ã€ãŠãæŒç®åã«ã€ããŠã®è»œã埩ç¿
ã»ã€ãŠãæŒç®åïŒ:=
ïŒã¯Python 3.8ã§å°å
¥ãããŸãããåŒã®äžã§å€æ°ã«å€ãå²ãåœãŠããå Žåã«åœ¹ç«ã¡ãŸãã
def some_func():
# ããã§é«ã³ã¹ãã®èšç®ãè¡ããããšããŸã
# time.sleep(1000)
return 5
# ãªã®ã§ã
if some_func():
print(some_func()) # èšç®ã2åè¡ãããã®ã¯è¯ããªãã§ãã
# ãããã¯
a = some_func()
if a:
print(a)
# ããã§ç°¡æœã«ã§ããŸã
if a := some_func():
print(a)
åºå (> 3.8):
5
5
5
ããã«ããã1è¡ã®ã³ãŒããçç¥ãããsome_func
ã2ååŒã³åºãããšãé²ãããŸããã
- æ¬åŒ§ãã€ããªãã代å
¥åŒãïŒã»ã€ãŠãæŒç®åã®äœ¿çšïŒã¯ãããã¬ãã«ã§ã¯å¶éãããŠãããããæåã®äŸã®
a := "wtf_walrus"
æã§SyntaxError
ãçºçããŸãããæ¬åŒ§ãã€ãããšæåŸ éãã«æ©èœããa
ã«å²ãåœãŠãããŸããã - éåžžã
=
æŒç®åãå«ãåŒã®æ¬åŒ§ä»ãã¯èš±å¯ãããŠããŸããããããã£ãŠã(a, b = 6, 9)
ã§ã®æ§æãšã©ãŒãçºçããŸãã - ã»ã€ãŠãæŒç®åã®æ§æã¯
NAME:= expr
ã®åœ¢åŒã§ãNAME
ã¯æå¹ãªèå¥åã§ãããexpr
ã¯æå¹ãªåŒã§ãããããã£ãŠãiterableã®ãããã³ã°ãšã¢ã³ãããã³ã°ã¯ãµããŒããããŠããŸãããã€ãŸãã(a := 6, 9)
ã¯((a := 6), 9)
ã«çžåœããæçµçã«ã¯(a, 9)
ã«ãªããŸãïŒããã§a
ã®å€ã¯6ã§ãïŒã
>>> (a := 6, 9) == ((a := 6), 9)
True
>>> x = (a := 696, 9)
>>> x
(696, 9)
>>> x[0] is a # ã©ã¡ããã¡ã¢ãªäžã®åãç®æãåç
§
True
åæ§ã«ã(a, b := 16, 19)
ã¯(a, (b := 16), 19)
ã«çžåœããããã¯ãã ã®3ã€çµã§ã
ⶠãã£ãããªæåå
>>> a = "some_string">>> id(a)
140420665652016
>>> id("some" + "_" + "string") # äž¡æ¹ã®idãåãã§ããããšã«æ³šæããŠãã ããã
140420665652016
>>> a = "wtf"
>>> b = "wtf"
>>> a is b
True
>>> a = "wtf!"
>>> b = "wtf!"
>>> a is b
False
>>> a, b = "wtf!", "wtf!">>> a is b # 3.7.xãé€ãå
šãŠã®ããŒãžã§ã³ã§
True
>>> a = "wtf!"; b = "wtf!">>> a is b # ããã¯ãã©ãã§å®è¡ãããã«ãã£ãŠTrueãŸãã¯FalseãåºåããŸãïŒPython shell / ipython / ã¹ã¯ãªãããšããŠïŒ
False
# ä»åºŠã¯some_file.pyãã¡ã€ã«å
ã§
a = "wtf!"
b = "wtf!"
print(a is b)
# ã¢ãžã¥ãŒã«ãåŒã³åºããšTrueãåºåãããŸãïŒ
åºå (< Python3.7 )
>>> 'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'
True
>>> 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'
False
çã«ããªã£ãŠããŸãããïŒ
ð¡ 解説:
-
æåãš2çªç®ã®ã¹ããããã®åäœã¯ãCPythonã®æé©åïŒæååã€ã³ã¿ãŒãã³ã°ãšåŒã°ããïŒã«ãããã®ã§ããããã€ãã®å Žåã«æ°ãããªããžã§ã¯ããäœæããã®ã§ã¯ãªããæ¢åã®äžå€ãªããžã§ã¯ãã䜿çšããããšããŸãã
-
ãã€ã³ã¿ãŒãã³ã°ããããåŸãå€ãã®å€æ°ãã¡ã¢ãªå ã®åãæååãªããžã§ã¯ããåç §ããããšããããŸãïŒããã«ããã¡ã¢ãªãç¯çŽã§ããŸãïŒã
-
äžèšã®ã¹ããããã§ã¯ãæååãæé»çã«ã€ã³ã¿ãŒãã³ã°ãããŠããŸããæååãæé»çã«ã€ã³ã¿ãŒãã³ã°ããããã©ããã®å€æã¯å®è£ ã«ãããŸããããã€ãã®ã«ãŒã«ãããã®ã§ãæååãã€ã³ã¿ãŒãã³ã°ããããã©ãããæšæž¬ããã®ã«ã䜿ããã ããïŒ
- é·ã0ãšé·ã1ã®ãã¹ãŠã®æååã¯ã€ã³ã¿ãŒãã³ã°ããã
- æååã¯ã³ã³ãã€ã«æã«ã€ã³ã¿ãŒãã³ã°ãããïŒ
'wtf'
ã¯ã€ã³ã¿ãŒãã³ã°ããããã''.join(['w', 't', 'f'])
ã¯ã€ã³ã¿ãŒãã³ã°ãããªãïŒ - ASCIIã®æåãæ°åãã¢ã³ããŒã¹ã³ã¢ã§æ§æãããŠããªãæååã¯ã€ã³ã¿ãŒãã³ã°ãããªããããã
'wtf!'
ãã€ã³ã¿ãŒãã³ã°ãããªãã£ãçç±ïŒ!
ã®ããïŒãïŒãã®ã«ãŒã«ã®CPythonå®è£ ã¯ãã¡ããã芧ãã ããïŒ
-
a
ãšb
ãåãè¡ã§"wtf!"
ã«èšå®ããããšãPythonã€ã³ã¿ãŒããªã¿ã¯æ°ãããªããžã§ã¯ããäœæãããã®åŸããã«2çªç®ã®å€æ°ãåç §ãããå¥ã ã®è¡ã§è¡ããšã"wtf!"
ãæ¢ã«ãªããžã§ã¯ããšããŠååšããŠããããšããç¥ããªããïŒäžèšã®äºå®ã«ããã°"wtf!"
ã¯æé»çã«ã€ã³ã¿ãŒãã³ã°ãããªãããïŒããããã³ã³ãã€ã«æã®æé©åãïŒãã®æé©åã¯CPythonã®3.7.xããŒãžã§ã³ã«ã¯é©çšãããŸããã詳现ãªè°è«ã«ã€ããŠã¯ãã®issueãã芧ãã ããïŒã -
察話åç°å¢ïŒIPythonãªã©ïŒã§ã¯ã³ã³ãã€ã«åäœã¯åäžã®ã¹ããŒãã¡ã³ãã§ãããã¢ãžã¥ãŒã«ã®å Žåã¯ã¢ãžã¥ãŒã«å šäœã
a, b = "wtf!", "wtf!"
ã¯åäžã®ã¹ããŒãã¡ã³ãã§ãããa = "wtf!"; b = "wtf!"
ã¯åäžè¡ã®2ã€ã®ã¹ããŒãã¡ã³ãããããããa = "wtf!"; b = "wtf!"
ã§èå¥åãç°ãªãçç±ã§ãããsome_file.py
ã§åŒã³åºãããå Žåã«åãã§ããçç±ã«ããªãã -
4çªç®ã®ã¹ããããã®åºåãçªç¶å€ããã®ã¯ãããŒãããŒã«æé©åãšããŠç¥ãããå®æ°æãããã¿ã«ãããã®ãã€ãŸããåŒ
'a'*20
ã¯ã³ã³ãã€ã«äžã«'aaaaaaaaaaaaaaaaaaaa'
ã«çœ®ãæããããã©ã³ã¿ã€ã äžã«ããã€ãã®ã¯ããã¯ãµã€ã¯ã«ãç¯çŽãããå®æ°æãããã¿ã¯ãé·ãã21æªæºã®æååã«å¯ŸããŠã®ã¿è¡ããããïŒãªãã§ããããïŒåŒ'a'*10**10
ã®çµæãšããŠçæããã.pyc
ãã¡ã€ã«ã®ãµã€ãºãæ³åããŠã¿ãŠãã ãããå®è£ ãœãŒã¹ã¯ãã¡ããïŒ -
泚æïŒPython 3.7ã§ã¯ãå®æ°æãããã¿ã¯ããŒãããŒã«æé©åããæ°ããªASTæé©åã«ç§»ãããããžãã¯ãå°ãå€æŽãããã®ã§ã4çªç®ã®ã¹ããããã¯Python 3.7ã§ã¯æ©èœããŸãããå€æŽã«ã€ããŠã®è©³çŽ°ã¯ãã¡ãã
ⶠé£éããæäœã«ã¯æ³šæãå¿ èŠ
>>> (False == False) in [False] # ããããŸã
False
>>> False == (False in [False]) # ããããŸã
False
>>> False == False in [False] # ãïŒ
True
>>> True is False == False
False
>>> False is False is False
True
>>> 1 > 0 < 1
True
>>> (1 > 0) < 1
False
>>> 1 > (0 < 1)
False
ð¡ 解説:
https://docs.python.org/3/reference/expressions.html#comparisons ã«ãããš
圢åŒçã«ã¯ãa, b, c, ..., y, zãåŒã§ãããop1, op2, ..., opNãæ¯èŒæŒç®åã§ããå Žåãa op1 b op2 c ... y opN zã¯ãa op1 bãã€b op2 cãã€...y opN zã«çžåœããŸãããååŒã¯æ倧ã§äžåºŠããè©äŸ¡ãããŸããã
äžèšã®äŸã§ã¯ãã®ãããªåäœãã°ãããŠãããšæãããããããŸããããa == b == c
ã0 <= x <= 100
ã®ãããªãã®ã«å¯ŸããŠã¯æ¬åœã«çŽ æŽãããã§ãã
-
False is False is False
ã¯(False is False) and (False is False)
ã«çžåœããŸãã -
True is False == False
ã¯(True is False) and (False == False)
ã«çžåœããæã®æåã®éšåïŒTrue is False
ïŒãFalse
ã«è©äŸ¡ããããããå šäœã®åŒãFalse
ã«è©äŸ¡ãããŸãã -
1 > 0 < 1
ã¯(1 > 0) and (0 < 1)
ã«çžåœããTrue
ã«è©äŸ¡ãããŸãã -
åŒ
(1 > 0) < 1
ã¯True < 1
ã«çžåœãã>>> int(True) 1 >>> True + 1 # ãã®äŸã«ã¯é¢ä¿ãããŸããããé¢çœãã§ãã 2
-
ãªã®ã§ã
1 < 1
ã¯False
ã«è©äŸ¡ãããŸãã
is
æŒç®åã®èœãšãç©Ž
ⶠ以äžã¯ãããäžã§ãéåžžã«æåãªäŸã§ãã
>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = []
>>> b = []
>>> a is b
False
>>> a = tuple()
>>> b = tuple()
>>> a is b
True
åºå
>>> a, b = 257, 257
>>> a is b
True
åºå (Python 3.7.xéå®)
>>> a, b = 257, 257
>>> a is b
False
ð¡ 解説:
is
ãš ==
ã®éã
-
is
æŒç®åã¯ãäž¡æ¹ã®ãªãã©ã³ããåããªããžã§ã¯ããåç §ããŠãããã©ããããã§ãã¯ããŸãïŒã€ãŸãããªãã©ã³ãã®IDãäžèŽãããã©ããããã§ãã¯ããŸãïŒã -
==
æŒç®åã¯ãäž¡æ¹ã®ãªãã©ã³ãã®å€ãæ¯èŒããŠãããããåãã§ããããã§ãã¯ããŸãã -
ã€ãŸã
is
ã¯åç §ã®ç䟡æ§ã®ããã®ãã®ã§ã==
ã¯å€ã®ç䟡æ§ã®ããã®ãã®ã§ããããããããäŸãæããŸãã>>> class A: pass >>> A() is A() # ããããç°ãªãã¡ã¢ãªäœçœ®ã®ç©ºã®ãªããžã§ã¯ãã§ãã False
256
ã¯æ¢åã®ãªããžã§ã¯ãã§ãã 257
ã¯ããã§ã¯ãããŸãã
Pythonãèµ·åãããšã-5
ãã 256
ãŸã§ã®æ°ãå²ãåœãŠãããŸãããããã®æ°ã¯é »ç¹ã«äœ¿çšãããã®ã§ãæºåããŠããã®ã¯çã«ããªã£ãŠããŸãã
https://docs.python.org/3/c-api/long.html ããã®åŒçš
çŸåšã®å®è£ ã§ã¯ã-5 ãã 256 ãŸã§ã®å šãŠã®æŽæ°ã«ã€ããŠãæŽæ°ãªããžã§ã¯ãã®é åãä¿æãããŠããŸãããã®ç¯å²ã§ int ãäœæãããšãæ¢åã®ãªããžã§ã¯ããžã®åç §ãè¿ãã ãã§ãããã®ããã1 ã®å€ãå€æŽããããšã¯çè«çã«å¯èœã§ããç§ã¯ããã®å Žåã®Pythonã®åäœã¯æªå®çŸ©ã ãšèããŠããŸãã:-)
>>> id(256)
10922528
>>> a = 256
>>> b = 256
>>> id(a)
10922528
>>> id(b)
10922528
>>> id(257)
140084850247312
>>> x = 257
>>> y = 257
>>> id(x)
140084850247440
>>> id(y)
140084850247344
ã€ã³ã¿ãŒããªã¿ã y = 257
ãå®è¡ããéã«ããã§ã« 257
ã®å€ã®æŽæ°ãäœæãããŠããããšãèªèã§ããã»ã©è³¢ããããŸããããã®ãããã¡ã¢ãªå
ã«å¥ã®ãªããžã§ã¯ããäœæããŠããŸããŸãã
åæ§ã®æé©åã¯ã空ã®ã¿ãã«ã®ãããªä»ã®äžå€ãªããžã§ã¯ãã«ãé©çšãããŸãããªã¹ãã¯å€æŽå¯èœãªã®ã§ã[] is []
㯠False
ãè¿ãã() is ()
㯠True
ãè¿ããŸããããã第2ã®ã¹ããããã®è§£èª¬ã§ãã第3ã®ã¹ããããã«é²ã¿ãŸãããã
åãè¡ã§åãå€ã§åæåãããå Žåãa
ãš b
ã¯åããªããžã§ã¯ããåç
§ããŸãã
åºå
>>> a, b = 257, 257
>>> id(a)
140640774013296
>>> id(b)
140640774013296
>>> a = 257
>>> b = 257
>>> id(a)
140640774013392
>>> id(b)
140640774013488
-
a ãš b ãåãè¡ã§
257
ã«èšå®ããããšãPythonã€ã³ã¿ãŒããªã¿ã¯æ°ãããªããžã§ã¯ããäœæãããã®åŸããã«2çªç®ã®å€æ°ãåç §ããŸããå¥ã ã®è¡ã§è¡ããšããã§ã«257
ããªããžã§ã¯ããšããŠååšããããšããç¥ããŸãããã -
ããã¯ã³ã³ãã€ã©ã®æé©åã§ãããç¹ã«å¯Ÿè©±åç°å¢ã«é©çšãããŸããã©ã€ãã€ã³ã¿ãŒããªã¿ã§2è¡å ¥åãããšãåå¥ã«ã³ã³ãã€ã«ããããããåå¥ã«æé©åãããŸãããã®äŸã
.py
ãã¡ã€ã«ã§è©Šããšããã¡ã€ã«ãäžåºŠã«ã³ã³ãã€ã«ããããããåãåäœã¯èŠãããŸããããã®æé©åã¯æŽæ°ã«éãããä»ã®äžå€ããŒã¿åã«ãé©çšãããŸããäŸãã°ããæååã¯ãã£ãããªæããããã§åãäžããäŸãšåãããæååãæµ®åå°æ°ç¹æ°ã§ãåäœããŸãã>>> a, b = 257.0, 257.0 >>> a is b True
-
ãªã Python 3.7 ã§ã¯ãããæ©èœããªãã®ã§ããããïŒæœè±¡çãªçç±ãšããŠã¯ããã®ãããªã³ã³ãã€ã©ã®æé©åãå®è£ ã«äŸåããããã§ãïŒã€ãŸããããŒãžã§ã³ãOSãªã©ã«ãã£ãŠå€æŽãããå¯èœæ§ãããããïŒãå ·äœçã«ã©ã®ãããªå®è£ ã®å€æŽãåé¡ãåŒãèµ·ãããã®ãã¯ãŸã 調æ»äžã§ãããææ°æ å ±ã¯ãã¡ãã®issueã§ç¢ºèªã§ããŸãã
ⶠããã·ã¥ãã©ãŠããŒ
some_dict = {}
some_dict[5.5] = "JavaScript"
some_dict[5.0] = "Ruby"
some_dict[5] = "Python"
åºå:
>>> some_dict[5.5]
"JavaScript"
>>> some_dict[5.0] # "Ruby"ã®ååšã"Python"ã«ãã£ãŠç Žå£ãããïŒ
"Python"
>>> some_dict[5]
"Python"
>>> complex_five = 5 + 0j>>> type(complex_five)
complex
>>> some_dict[complex_five]
"Python"
ãªã"Python"ã ãããªã®ã§ããããïŒ
ð¡ 解説:
-
Pythonã®èŸæžã«ãããããŒã®äžææ§ã¯ãåäžæ§ã§ã¯ãªãç䟡æ§ã«åºã¥ããŠããŸãããããã£ãŠã
5
ã5.0
ã5 + 0j
ã¯ç°ãªãåã®å¥ã ã®ãªããžã§ã¯ãã§ããããããã¯çãããããåæã«åãdict
ïŒãŸãã¯set
ïŒã«ååšããããšã¯ã§ããŸãããããããäžã€ãæ¿å ¥ããåŸã«ä»ã®ç䟡ã§ã¯ãããã©ç°ãªãããŒã§æ€çŽ¢ãè©Šã¿ãŠããKeyError
ã§å€±æãã代ããã«ãå ã®ããããããå€ãæåããããã«ãªããŸãã>>> 5 == 5.0 == 5 + 0j True >>> 5 is not 5.0 is not 5 + 0j True >>> some_dict = {} >>> some_dict[5.0] = "Ruby">>> 5.0 in some_dict True >>> (5 in some_dict) and (5 + 0j in some_dict) True
ããã¯ã¢ã€ãã ãèšå®ããå Žåã«ãé©çšãããŸãããããã£ãŠãsome_dict[5] = "Python"
ãè¡ããšãPythonã¯ç䟡ãªã㌠5.0 -> "Ruby"
ã§æ¢åã®ã¢ã€ãã ãèŠã€ãããã®å Žã§å€ãäžæžãããå
ã®ããŒã¯ãã®ãŸãŸã«ããŸãã
>>> some_dict
{5.0: 'Ruby'}
>>> some_dict[5] = "Python"
>>> some_dict
{5.0: 'Python'}
-
ã§ã¯ãããŒã
5.0
ãã5
ã«æŽæ°ããã«ã¯ã©ãããã°ããã®ã§ããããïŒå®éã«ã¯ãã®å Žã§ã®æŽæ°ã¯ã§ããŸãããããŸãããŒãåé€ãïŒdel some_dict[5.0]
ïŒãããããèšå®ããïŒsome_dict[5]
ïŒããšã§ãæµ®åå°æ°ç¹ã®5.0
ã§ã¯ãªãæŽæ°5
ãããŒãšããŠååŸããããšãã§ããŸãããã ãããããå¿ èŠãšãªãã±ãŒã¹ã¯çšã§ãããã -
Pythonã¯ã©ããã£ãŠ
5.0
ãå«ãèŸæžã§5
ãèŠã€ããã®ã§ããããïŒããã·ã¥é¢æ°ã䜿çšããŠããã¹ãŠã®ã¢ã€ãã ãã¹ãã£ã³ããããšãªãå®æ°æéã§ãããè¡ããŸããPythonãèŸæžã§ããŒfoo
ãæ¢ããšãããŸãhash(foo)
ãèšç®ããŸãïŒããã¯å®æ°æéã§å®è¡ãããŸãïŒãçãããªããžã§ã¯ãã¯åãããã·ã¥å€ãæã€ããšãèŠæ±ãããŠããããïŒãã¡ãã®ããã¥ã¡ã³ãåç §ïŒã5
ã5.0
ã5 + 0j
ã¯åãããã·ã¥å€ãæã¡ãŸãã>>> 5 == 5.0 == 5 + 0j True >>> hash(5) == hash(5.0) == hash(5 + 0j) True
泚: éã¯å¿ ãããçã§ã¯ãããŸãããããã·ã¥å€ãçãããªããžã§ã¯ãã§ããããèªäœãçãããªãå ŽåããããŸããïŒããã¯ããã·ã¥è¡çªãšããŠç¥ãããéåžžããã·ã¥ãæäŸããå®æ°æéã®ããã©ãŒãã³ã¹ãäœäžãããŸããïŒ
ⶠæ¬è³ªçã«ã¯ã¿ããªåã
class WTF:
pass
åºå:
>>> WTF() == WTF() # äºã€ã®ç°ãªãã€ã³ã¹ã¿ã³ã¹ã¯çãããªã
False
>>> WTF() is WTF() # IDãç°ãªã
False
>>> hash(WTF()) == hash(WTF()) # ããã·ã¥å€ãç°ãªãã¹ã
True
>>> id(WTF()) == id(WTF())
True
ð¡ 解説:
-
id
ãåŒã³åºããããšããPythonã¯WTF
ã¯ã©ã¹ã®ãªããžã§ã¯ããäœæããid
é¢æ°ã«æž¡ããŸãããid
é¢æ°ã¯ãã®id
ïŒã¡ã¢ãªäœçœ®ïŒãååŸãããªããžã§ã¯ããç Žæ£ããŸãããªããžã§ã¯ãã¯ç Žæ£ãããŸãã -
ãããé£ç¶ããŠäºåè¡ããšãPythonã¯äºçªç®ã®ãªããžã§ã¯ãã«ãåãã¡ã¢ãªäœçœ®ãå²ãåœãŠãŸããCPythonã«ãããŠ
id
ã¯ãªããžã§ã¯ãã®IDãšããŠã¡ã¢ãªäœçœ®ã䜿çšããã®ã§ãäºã€ã®ãªããžã§ã¯ãã®IDã¯åãã«ãªããŸãã -
ãããã£ãŠããªããžã§ã¯ãã®IDã¯ãªããžã§ã¯ãã®çåæéã«å¯ŸããŠã®ã¿äžæã§ãããªããžã§ã¯ããç Žæ£ãããåŸããŸãã¯äœæãããåã§ããã°ãä»ã®äœããåãIDãæã€ããšããããŸãã
-
ã§ã¯ããªã
is
æŒç®åã¯False
ãšè©äŸ¡ãããã®ã§ããããïŒãã¡ãã®ã¹ããããã§ç¢ºèªããŸããããclass WTF(object): def __init__(self): print("I") def __del__(self): print("D")
åºå:
>>> WTF() is WTF()
I
I
D
D
False
>>> id(WTF()) == id(WTF())
I
D
I
D
True
- ã芧ã®éãããªããžã§ã¯ããç Žæ£ãããé çªããããã§ã®éããçãã§ããŸãã
ⶠ秩åºã®äžã®ç¡ç§©åº
from collections import OrderedDict
dictionary = dict()
dictionary[1] = 'a'; dictionary[2] = 'b';
ordered_dict = OrderedDict()
ordered_dict[1] = 'a'; ordered_dict[2] = 'b';
another_ordered_dict = OrderedDict()
another_ordered_dict[2] = 'b'; another_ordered_dict[1] = 'a';
class DictWithHash(dict):
"""
A dict that also implements __hash__ magic.
"""
__hash__ = lambda self: 0
class OrderedDictWithHash(OrderedDict):
"""
An OrderedDict that also implements __hash__ magic.
"""
__hash__ = lambda self: 0
åºå
>>> dictionary == ordered_dict # a == b ã§ããã
True
>>> dictionary == another_ordered_dict # b == c ã§ããã°ã
True
>>> ordered_dict == another_ordered_dict # ãªã c == a ã§ã¯ãªãã®ãïŒïŒ
False
# setã¯ãŠããŒã¯ãªèŠçŽ ã®ã¿ããæ§æãããããšãç¥ã£ãŠããŸããã
# ãããã®èŸæžã䜿ã£ãŠãäœã£ãŠã¿ããšã©ããªãã§ããã...
>>> len({dictionary, ordered_dict, another_ordered_dict})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
# èŸæžã«ã¯ __hash__ ãå®è£
ãããŠããªãã®ã§çã«ããªã£ãŠããŸããã
# ã©ãããŒã¯ã©ã¹ã䜿ããŸãããã
>>> dictionary = DictWithHash()
>>> dictionary[1] = 'a'; dictionary[2] = 'b';
>>> ordered_dict = OrderedDictWithHash()
>>> ordered_dict[1] = 'a'; ordered_dict[2] = 'b';
>>> another_ordered_dict = OrderedDictWithHash()
>>> another_ordered_dict[2] = 'b'; another_ordered_dict[1] = 'a';
>>> len({dictionary, ordered_dict, another_ordered_dict})
1
>>> len({ordered_dict, another_ordered_dict, dictionary}) # é çªãå€ããŠããŸã
2
ããã§ã¯äœãèµ·ããŠããã®ã§ããããïŒ
ð¡ 解説:
-
dictionary
ãordered_dict
ãanother_ordered_dict
ã®éã§æšç§»çç䟡æ§ãä¿æãããªãã£ãçç±ã¯ãOrderedDict
ã¯ã©ã¹ã§__eq__
ã¡ãœãããã©ã®ããã«å®è£ ãããŠãããã«ãããŸããããã¥ã¡ã³ãããã®åŒçšã§ããOrderedDictãªããžã§ã¯ãéã®ç䟡æ§ãã¹ãã¯é åºã«ææã§ãããlist(od1.items())==list(od2.items()) ãšããŠå®è£ ãããŠããŸããOrderedDictãªããžã§ã¯ããšä»ã®ãããã³ã°ãªããžã§ã¯ãéã®ç䟡æ§ãã¹ãã¯ãéåžžã®èŸæžã®ããã«é åºã«ææã§ã¯ãããŸããã
-
ãã®åäœã®ç䟡æ§ã®çç±ã¯ã
OrderedDict
ãªããžã§ã¯ããéåžžã®èŸæžã䜿çšãããå Žæã§çŽæ¥ä»£çšã§ããããã«ããããã§ãã -
ã§ã¯ããªãé åºãå€ãããšçæããã
set
ãªããžã§ã¯ãã®é·ãã«åœ±é¿ãåºãã®ã§ããããïŒçãã¯æšç§»çç䟡æ§ã®æ¬ åŠã«ä»ãªããŸãããsetã¯ãŠããŒã¯ãªèŠçŽ ã®ãé åºãªããã³ã¬ã¯ã·ã§ã³ãªã®ã§ãèŠçŽ ãæ¿å ¥ãããé åºã¯é¢ä¿ãªãã¯ãã§ãããããããã®å Žåã¯é¢ä¿ãããŸããããå°ã詳ããå解ããŠã¿ãŸãããã
>>> some_set = set()
>>> some_set.add(dictionary) # ããã¯äžèšã¹ããããããã®ãããã³ã°ãªããžã§ã¯ãã§ã
>>> ordered_dict in some_set
True
>>> some_set.add(ordered_dict)
>>> len(some_set)
1
>>> another_ordered_dict in some_set
True
>>> some_set.add(another_ordered_dict)
>>> len(some_set)
1
>>> another_set = set()
>>> another_set.add(ordered_dict)
>>> another_ordered_dict in another_set
False
>>> another_set.add(another_ordered_dict)
>>> len(another_set)
2
>>> dictionary in another_set
True
>>> another_set.add(another_ordered_dict)
>>> len(another_set)
2
- ã€ãŸããäžäžèŽã¯
another_ordered_dict in another_set
ãFalse
ãšãªãããšã«ãããã®ãªã®ã§ãããªããªããanother_set
ã«ã¯æ¢ã«ordered_dict
ãååšããŠãããå ã»ã©èŠãããã«ordered_dict == another_ordered_dict
ã¯False
ãªã®ã§ãã
ⶠãã©ã€ãç¶ãããïŒ
def some_func():
try:
return 'from_try'
finally:
return 'from_finally'
def another_func():
for _ in range(3):
try:
continue
finally:
print("Finally!")
def one_more_func(): # ããïŒ
try:
for i in range(3):
try:
1 / i
except ZeroDivisionError:
# ããã§äŸå€ãæããŠãforã«ãŒãã®å€ã§åŠçããŸããã
raise ZeroDivisionError("A trivial divide by zero error")
finally:
print("Iteration", i)
break
except ZeroDivisionError as e:
print("Zero division error occurred", e)
åºå:
>>> some_func()
'from_finally'
>>> another_func()
Finally!
Finally!
Finally!
>>> 1 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> one_more_func()
Iteration 0
ð¡ 解説:
try
ãããã¯å ã§return
,break
,continue
æãå®è¡ãããå Žåãfinally
ç¯ãåºããšãã«å®è¡ãããŸãã- é¢æ°ã®æ»ãå€ã¯ãæåŸã«å®è¡ããã
return
æã«ãã£ãŠæ±ºå®ãããŸããfinally
ç¯ã¯åžžã«å®è¡ãããã®ã§ãfinally
ç¯å ã§å®è¡ãããreturn
æãåžžã«æåŸã«å®è¡ããããã®ã«ãªããŸãã - ããã§ã®æ³šæç¹ã¯ã
finally
ç¯ãreturn
ãŸãã¯break
æãå®è¡ãããšãäžæçã«ä¿åãããŠããäŸå€ãç Žæ£ãããããšã§ãã
ⶠäœã®ãããªã®ã
some_string = "wtf"
some_dict = {}
for i, some_dict[i] in enumerate(some_string):
i = 10
åºå:
>>> some_dict # ã€ã³ããã¯ã¹ä»ãã®dictãçŸããã
{0: 'w', 1: 't', 2: 'f'}
ð¡ 解説:
-
Pythonææ³ã«ããã
for
æã¯æ¬¡ã®ããã«å®çŸ©ãããŠããŸãïŒfor_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
ããã§exprlist
ã¯ä»£å
¥ã®ã¿ãŒã²ããã§ããã€ãŸãã{exprlist} = {next_value}
ãå埩å¯èœãªåã¢ã€ãã ã«å¯ŸããŠå®è¡ãããããšãæå³ããŸããåãããããããããã«ãèå³æ·±ãäŸãèŠãŠã¿ãŸãããïŒ
for i in range(4):
print(i)
i = 10
åºå:
0
1
2
3
ã«ãŒãã¯äžåºŠããå®è¡ãããªããšæããŸãããïŒ
ð¡ 解説:
-
i = 10
ã®ä»£å ¥æãã«ãŒãã®å埩ã«åœ±é¿ãäžããªãã®ã¯ãPythonã®forã«ãŒãã®åäœæ¹æ³ã«ãããã®ã§ããåå埩ã®éå§åã«ãã€ãã¬ãŒã¿ã«ãã£ãŠæäŸããã次ã®ã¢ã€ãã ïŒãã®å Žåã¯range(4)
ïŒãã¢ã³ããã¯ãããã¿ãŒã²ãããªã¹ãå€æ°ã«å²ãåœãŠãããŸãïŒãã®å Žåã¯i
ïŒã -
enumerate(some_string)
é¢æ°ã¯ãåå埩ã§æ°ããå€i
ïŒå¢å ããã«ãŠã³ã¿ïŒãšsome_string
ããã®æåãçæããŸãããããããèŸæžsome_dict
ã®i
ããŒããã®æåã«èšå®ããŸããã«ãŒãã®å±éãåçŽåãããšä»¥äžã®ããã«ãªããŸãïŒ>>> i, some_dict[i] = (0, 'w') >>> i, some_dict[i] = (1, 't') >>> i, some_dict[i] = (2, 'f') >>> some_dict
ⶠè©äŸ¡æéã®äžäžèŽ
array = [1, 8, 15]
# å
žåçãªãžã§ãã¬ãŒã¿åŒ
gen = (x for x in array if array.count(x) > 0)
array = [2, 8, 22]
åºå:
>>> print(list(gen)) # ä»ã®å€ã¯ã©ããžïŒ
[8]
array_1 = [1,2,3,4]
gen_1 = (x for x in array_1)
array_1 = [1,2,3,4,5]
array_2 = [1,2,3,4]
gen_2 = (x for x in array_2)
array_2[:] = [1,2,3,4,5]
åºå:
>>> print(list(gen_1))
[1, 2, 3, 4]
>>> print(list(gen_2))
[1, 2, 3, 4, 5]
array_3 = [1, 2, 3]
array_4 = [10, 20, 30]
gen = (i + j for i in array_3 for j in array_4)
array_3 = [4, 5, 6]
array_4 = [400, 500, 600]
åºå:
>>> print(list(gen))
[401, 501, 601, 402, 502, 602, 403, 503, 603]
ð¡ 解説:
-
ãžã§ãã¬ãŒã¿åŒã«ãããŠã
in
å¥ã¯å®£èšæã«è©äŸ¡ãããŸãããæ¡ä»¶å¥ã¯å®è¡æã«è©äŸ¡ãããŸãã -
ãããã£ãŠãå®è¡æã«ãªãåã«
array
ã¯ãªã¹ã[2, 8, 22]
ã«å床å²ãåœãŠããã1
,8
,15
ã®ãã¡8
ã®ã«ãŠã³ãã ãã0ãã倧ããã®ã§ããžã§ãã¬ãŒã¿ã¯8
ã®ã¿ãçæããŸãã -
gen_1
ãšgen_2
ã®åºåã®éãã¯ãå€æ°array_1
ãšarray_2
ã«å€ãåå²ãåœãŠãããæ¹æ³ã«ãã£ãŠçãããã®ã§ãã -
æåã®ã±ãŒã¹ã§ã¯ã
array_1
ã¯æ°ãããªããžã§ã¯ã[1,2,3,4,5]
ã«ãã€ã³ããããin
å¥ã¯å®£èšæã«è©äŸ¡ãããã®ã§ãå€ããªããžã§ã¯ã[1,2,3,4]
ïŒç Žæ£ãããŠããªãïŒãæãç¶ããŸãã -
äºã€ç®ã®ã±ãŒã¹ã§ã¯ã
array_2
ãžã®ã¹ã©ã€ã¹ä»£å ¥ãå€ããªããžã§ã¯ã[1,2,3,4]
ã[1,2,3,4,5]
ã«æŽæ°ããŸãããããã£ãŠãgen_2
ãšarray_2
ã®äž¡æ¹ãåããªããžã§ã¯ãïŒçŸåšã¯[1,2,3,4,5]
ã«æŽæ°ãããŠããïŒãåŒãç¶ãåç §ããŠããŸãã -
ã§ã¯ããããŸã§ã®ããžãã¯ã«åºã¥ãã第3ã¹ããããã®
list(gen)
ã®å€ã¯[11, 21, 31, 12, 22, 32, 13, 23, 33]
ã§ããã¹ãã§ã¯ãªãã§ããããïŒïŒãªããªãarray_3
ãšarray_4
ã¯array_1
ã®ããã«æ¯ãèãã¯ãã§ãïŒããªãïŒarray_4
ã®å€ã®ã¿ãæŽæ°ãããã®ãã¯ãPEP-289ã§è§£èª¬ãããŠããŸããæãå€åŽã®foråŒã ããããã«è©äŸ¡ãããä»ã®åŒã¯ãžã§ãã¬ãŒã¿ãå®è¡ããããŸã§é 延ãããã
ð¡ 解説:
ãžã§ãã¬ãŒã¿åŒã«ãããŠãinå¥ã¯å®£èšæã«è©äŸ¡ãããŸãããæ¡ä»¶å¥ã¯å®è¡æã«è©äŸ¡ãããŸãã
ãããã£ãŠãå®è¡æã«ãªãåã«arrayã¯ãªã¹ã[2, 8, 22]ã«å床å²ãåœãŠããã1, 8, 15ã®ãã¡8ã®ã«ãŠã³ãã ãã0ãã倧ããã®ã§ããžã§ãã¬ãŒã¿ã¯8ã®ã¿ãçæããŸãã
gen_1ãšgen_2ã®åºåã®éãã¯ãå€æ°array_1ãšarray_2ã«å€ãåå²ãåœãŠãããæ¹æ³ã«ãã£ãŠçãããã®ã§ãã
æåã®ã±ãŒã¹ã§ã¯ãarray_1ã¯æ°ãããªããžã§ã¯ã[1,2,3,4,5]ã«ãã€ã³ããããinå¥ã¯å®£èšæã«è©äŸ¡ãããã®ã§ãå€ããªããžã§ã¯ã[1,2,3,4]ïŒç Žæ£ãããŠããªãïŒãæãç¶ããŸãã
äºã€ç®ã®ã±ãŒã¹ã§ã¯ãarray_2ãžã®ã¹ã©ã€ã¹ä»£å
¥ãå€ããªããžã§ã¯ã[1,2,3,4]ã[1,2,3,4,5]ã«æŽæ°ããŸãããããã£ãŠãgen_2ãšarray_2ã®äž¡æ¹ãåããªããžã§ã¯ãïŒçŸåšã¯[1,2,3,4,5]ã«æŽæ°ãããŠããïŒãåŒãç¶ãåç
§ããŠããŸãã
ã§ã¯ããããŸã§ã®ããžãã¯ã«åºã¥ãã第3ã¹ããããã®list(gen)ã®å€ã¯[11, 21, 31, 12, 22, 32, 13, 23, 33]ã§ããã¹ãã§ã¯ãªãã§ããããïŒïŒãªããªãarray_3ãšarray_4ã¯array_1ã®ããã«æ¯ãèãã¯ãã§ãïŒããªãïŒarray_4ã®å€ã®ã¿ãæŽæ°ãããã®ãã¯ãPEP-289ã§è§£èª¬ãããŠããŸãã
æãå€åŽã®foråŒã ããããã«è©äŸ¡ãããä»ã®åŒã¯ãžã§ãã¬ãŒã¿ãå®è¡ããããŸã§é
延ãããã
ⶠis not ... 㯠is (not ...) ã§ã¯ãªã
>>> 'something' is not None
True
>>> 'something' is (not None)
False
ð¡ 解説:
is notã¯äžã€ã®äºé
æŒç®åã§ãisãšnotãåããŠäœ¿ããšããšã¯åäœãç°ãªããŸãã
is notã¯ããªãã¬ãŒã¿ã®äž¡åŽã®å€æ°ãåããªããžã§ã¯ããæããŠããå Žåã¯Falseã«ãããã§ãªãå Žåã¯Trueã«è©äŸ¡ãããŸãã
äŸã§ã¯ã(not None)ã¯Trueã«è©äŸ¡ãããŸãããªããªãNoneã®å€ã¯ããŒã«ã³ã³ããã¹ãã§Falseã«ãªãã®ã§ãåŒã¯'something' is Trueã«ãªããŸãã
ⶠäžçºã§Xãåã€äžç®äžŠã¹ïŒ
# è¡ãåæå
row = [""] * 3 #è¡ i['', '', '']
# ç€é¢ãäœã
board = [row] * 3
åºå:
>>> board
[['', '', ''], ['', '', ''], ['', '', '']]
>>> board[0]
['', '', '']
>>> board[0][0]
''
>>> board[0][0] = "X"
>>> board
[['X', '', ''], ['X', '', ''], ['X', '', '']]
"X"
ã3ã€å²ãåœãŠãããã§ã¯ãããŸããããïŒ
ð¡ 解説:
row
å€æ°ãåæåãããšãã¡ã¢ãªå
ã§ä»¥äžã®ããã«ãªããŸãã
ãããŠãrow
ãä¹ç®ããŠboard
ãåæåããããšãã¡ã¢ãªå
ã§ä»¥äžã®ããã«ãªããŸãïŒboard[0]
, board[1]
, board[2]
ã®åèŠçŽ ã¯ãrow
ãåç
§ããŠããåããªã¹ããåç
§ïŒã
ãã®ã·ããªãªãé¿ããã«ã¯ãrow
å€æ°ã䜿ã£ãŠboard
ãçæããªãããã«ããŸãïŒãã®issueã§å°ããããŸããïŒã
>>> board = [['']*3 for _ in range(3)]
>>> board[0][0] = "X"
>>> board
[['X', '', ''], ['', '', ''], ['', '', '']]
ⶠã·ã¥ã¬ãŒãã£ã³ã¬ãŒã®å€æ°
funcs = []
results = []
for x in range(7):
def some_func():
return x
funcs.append(some_func)
results.append(some_func()) # ããã§é¢æ°ãåŒã³åºããŠããããšã«æ³šæ
funcs_results = [func() for func in funcs]
åºå (PythonããŒãžã§ã³):
>>> results
[0, 1, 2, 3, 4, 5, 6]
>>> funcs_results
[6, 6, 6, 6, 6, 6, 6]
ã«ãŒãäžã«some_func
ãfuncs
ã«è¿œå ããåã«x
ã®å€ãæ¯åç°ãªã£ãŠããŸããããã«ãŒããå®äºããåŸã«è©äŸ¡ããããšãã¹ãŠã®é¢æ°ã6ãè¿ããŸãã
>>> powers_of_x = [lambda x: x**i for i in range(10)]
>>> [f(2) for f in powers_of_x]
[512, 512, 512, 512, 512, 512, 512, 512, 512, 512]
ð¡ 解説
- ã«ãŒãå
ã§é¢æ°ãå®çŸ©ãããã®é¢æ°ã®æ¬äœã§ã«ãŒãå€æ°ã䜿çšããå Žåãã«ãŒãé¢æ°ã®ã¯ããŒãžã£ã¯å€æ°ã«ãã€ã³ããããŸãããã®å€ã«ã¯ãã€ã³ããããŸãããé¢æ°ã¯ãã®å€ã䜿çšããã®ã§ã¯ãªããåšå²ã®ã³ã³ããã¹ããã
x
ãæ¢ããŸãããã®ããããã¹ãŠã®é¢æ°ã¯èšç®ã®ããã«å€æ°ã«å²ãåœãŠãããææ°ã®å€ã䜿çšããŸãã以äžãåšå²ã®ã³ã³ããã¹ãããã®x
ã䜿çšããŠããããšïŒã€ãŸããããŒã«ã«å€æ°ã§ã¯ãªãããšïŒãã確èªãã ããã
>>> import inspect
>>> inspect.getclosurevars(funcs[0])
ClosureVars(nonlocals={}, globals={'x': 6}, builtins={}, unbound=set())
x
ã¯ã°ããŒãã«ãªå€ãªã®ã§ãx
ãæŽæ°ããããšã§funcs
ãåç
§ããŠè¿ãå€ãå€æŽããããšãã§ããŸãã
>>> x = 42>>> [func() for func in funcs]
[42, 42, 42, 42, 42, 42, 42]
- æãŸããåäœãåŸãããã«ã¯ãã«ãŒãå€æ°ãååä»ãå€æ°ãšããŠé¢æ°ã«æž¡ããŸãããããªããããæ©èœããã®ãïŒ ããã¯ãå€æ°ãé¢æ°ã®ã¹ã³ãŒãå
ã§å®çŸ©ããããã§ãããã¯ãåšå²ã®ïŒã°ããŒãã«ïŒã¹ã³ãŒããæ¢ããŠå€æ°ã®å€ãæ¢ãã®ã§ã¯ãªãããã®æç¹ã§ã®
x
ã®å€ãæ ŒçŽããããŒã«ã«å€æ°ãäœæããŸãã
funcs = []
for x in range(7):
def some_func(x=x):
return x
funcs.append(some_func)
åºå:
>>> funcs_results = [func() for func in funcs]
>>> funcs_results
[0, 1, 2, 3, 4, 5, 6]
ããã°ããŒãã«ã¹ã³ãŒãã®x
ã¯äœ¿çšããŠããŸããã
>>> inspect.getclosurevars(funcs[0])
ClosureVars(nonlocals={}, globals={}, builtins={}, unbound=set())
ⶠé¶ãå ãåµãå ã
>>> isinstance(3, int)
True
>>> isinstance(type, object)
True
>>> isinstance(object, type)
True
ã§ã¯ã"究極"ã®åºåºã¯ã©ã¹ã¯ã©ãã§ããããïŒããã«...
>>> class A: pass>>> isinstance(A, A)
False
>>> isinstance(type, type)
True
>>> isinstance(object, object)
True
>>> issubclass(int, object)
True
>>> issubclass(type, object)
True
>>> issubclass(object, type)
False
ð¡ 解説
- Pythonã«ãããŠ
type
ã¯ã¡ã¿ã¯ã©ã¹ã§ãã - ãã¹ãŠã®ãã®ãPythonã§ã¯ãªããžã§ã¯ãã§ããã¯ã©ã¹ãšãã®ãªããžã§ã¯ãïŒã€ã³ã¹ã¿ã³ã¹ïŒãå«ãŸããŸãã
type
ã¯ã©ã¹ã¯object
ã¯ã©ã¹ã®ã¡ã¿ã¯ã©ã¹ã§ãããtype
ãå«ããã¹ãŠã®ã¯ã©ã¹ã¯çŽæ¥çã«ãéæ¥çã«ãobject
ããç¶æ¿ããŠããŸããobject
ãštype
ã®éã«å®éã®åºåºã¯ã©ã¹ã¯ååšããŸãããäžèšã®ã¹ããããã§ã®æ··ä¹±ã¯ãPythonã®ã¯ã©ã¹ãšãã芳ç¹ãããããã®é¢ä¿ïŒissubclass
ãisinstance
ïŒãèããŠããããã§ããobject
ãštype
ã®é¢ä¿ã¯çŽç²ãªPythonã§ã¯åçŸã§ããŸãããã€ãŸãã以äžã®ãããªé¢ä¿ã¯çŽç²ãªPythonã§ã¯åçŸã§ããŸããã- ã¯ã©ã¹Aã¯ã¯ã©ã¹Bã®ã€ã³ã¹ã¿ã³ã¹ã§ãããã¯ã©ã¹Bã¯ã¯ã©ã¹Aã®ã€ã³ã¹ã¿ã³ã¹ã§ããã
- ã¯ã©ã¹Aã¯èªèº«ã®ã€ã³ã¹ã¿ã³ã¹ã§ããã
object
ãštype
ã®ãã®ãããªé¢ä¿ïŒãäºãã®ã€ã³ã¹ã¿ã³ã¹ã§ãããªããèªèº«ãã€ã³ã¹ã¿ã³ã¹ã§ããïŒã¯ãå®è£ ã¬ãã«ã§ã®"ããŸãã"ã«ãã£ãŠPythonã«ååšããŠããŸãã
ⶠãµãã¯ã©ã¹ã®ãªã¬ãŒã·ã§ã³
åºå:
>>> from collections.abc import Hashable
>>> issubclass(list, object)
True
>>> issubclass(object, Hashable)
True
>>> issubclass(list, Hashable)
False
ãµãã¯ã©ã¹ã®ãªã¬ãŒã·ã§ã³ã¯æšç§»çã§ãããšäºæ³ãããŠããŸããããïŒïŒA
ãB
ã®ãµãã¯ã©ã¹ã§ãB
ãC
ã®ãµãã¯ã©ã¹ãªãã°ãA
ãC
ã®ãµãã¯ã©ã¹ã§ããã¯ãã§ãïŒ
ð¡ 解説:
- Pythonã«ããããµãã¯ã©ã¹ã®ãªã¬ãŒã·ã§ã³ã¯å¿
ãããæšç§»çã§ã¯ãããŸãããã¡ã¿ã¯ã©ã¹ã§ç¬èªã®ä»»æã®
__subclasscheck__
ãå®çŸ©ããããšãèš±ãããŠããŸãã issubclass(cls, Hashable)
ãåŒã³åºããããšãåã«cls
ãŸãã¯ãããç¶æ¿ãããã®ã®äžã§åœã§ãªã"__hash__
"ã¡ãœãããæ¢ããŸããobject
ã¯ããã·ã¥å¯èœã§ãããlist
ã¯ããã·ã¥äžå¯èœãªã®ã§ãæšç§»çé¢ä¿ã厩ããŸãã- ãã詳ãã説æã¯ãã¡ã
ⶠã¡ãœããã®ç䟡æ§ãšåäžæ§
class SomeClass:
def method(self):
pass
@classmethod
def classm(cls):
pass
@staticmethod
def staticm():
pass
åºå:
>>> print(SomeClass.method is SomeClass.method)
True
>>> print(SomeClass.classm is SomeClass.classm)
False
>>> print(SomeClass.classm == SomeClass.classm)
True
>>> print(SomeClass.staticm is SomeClass.staticm)
True
classm
ã«äºåã¢ã¯ã»ã¹ãããšãåäžã§ã¯ãªãç䟡ãªãªããžã§ã¯ããåŸãããŸããSomeClass
ã®ã€ã³ã¹ã¿ã³ã¹ã§ã¯äœãèµ·ãããèŠãŠã¿ãŸãããïŒ
o1 = SomeClass()
o2 = SomeClass()
åºå:
>>> print(o1.method == o2.method)
False
>>> print(o1.method == o1.method)
True
>>> print(o1.method is o1.method)
False
>>> print(o1.classm is o1.classm)
False
>>> print(o1.classm == o1.classm == o2.classm == SomeClass.classm)
True
>>> print(o1.staticm is o1.staticm is o2.staticm is SomeClass.staticm)
True
SomeClass
ã®åãã€ã³ã¹ã¿ã³ã¹ã«å¯ŸããŠclassm
ãmethod
ã«äºåã¢ã¯ã»ã¹ãããšãç䟡ã§ã¯ãããåäžã§ã¯ãªããªããžã§ã¯ããçæãããŸãã
ð¡ 解説:
- é¢æ°ã¯ãã¹ã¯ãªãã¿ã§ããå±æ§ãšããŠé¢æ°ã«ã¢ã¯ã»ã¹ãããšãã¯ãã€ã§ãããã¹ã¯ãªãã¿ãåŒã³åºãããå±æ§ãææãããªããžã§ã¯ããšé¢æ°ãããã€ã³ããããã¡ãœãããªããžã§ã¯ããäœæãããŸããåŒã³åºãããå Žåãã¡ãœããã¯é¢æ°ãåŒã³åºããæé»ã®ãã¡ã«ãã€ã³ãããããªããžã§ã¯ããæåã®åŒæ°ãšããŠæž¡ããŸãïŒããããæ瀺çã«æž¡ããªããŠã
self
ãæåã®åŒæ°ãšããŠåŸãããæ¹æ³ã§ãïŒã
>>> o1.method
<bound method SomeClass.method of <__main__.SomeClass object at ...>>
- å±æ§ã«è€æ°åã¢ã¯ã»ã¹ãããšãæ¯åã¡ãœãããªããžã§ã¯ããäœæãããŸãïŒãããã£ãŠã
o1.method is o1.method
ãçã«ãªãããšã¯ãããŸãããã€ã³ã¹ã¿ã³ã¹ã§ã¯ãªãã¯ã©ã¹å±æ§ãšããŠé¢æ°ã«ã¢ã¯ã»ã¹ãããšã¡ãœããã¯äœæãããªãã®ã§ãSomeClass.method is SomeClass.method
ã¯çã§ãã
>>> SomeClass.method
<function SomeClass.method at ...>
classmethod
ã¯é¢æ°ãã¯ã©ã¹ã¡ãœããã«å€æããŸããã¯ã©ã¹ã¡ãœããã¯ãã¹ã¯ãªãã¿ã§ãããã¢ã¯ã»ã¹ããããšããªããžã§ã¯ãèªäœã§ã¯ãªããªããžã§ã¯ãã®ã¯ã©ã¹ïŒåïŒããã€ã³ãããã¡ãœãããªããžã§ã¯ããäœæããŸãã
>>> o1.classm
<bound method SomeClass.classm of <class '__main__.SomeClass'>>
- é¢æ°ãšã¯ç°ãªãã
classmethod
ã¯ã¯ã©ã¹å±æ§ãšããŠã¢ã¯ã»ã¹ãããå Žåã«ãã¡ãœãããäœæããŸãïŒãã®å Žåãããã¯ã¯ã©ã¹ã«ãã€ã³ããããåã«ã¯ãã€ã³ããããŸããïŒããããã£ãŠSomeClass.classm is SomeClass.classm
ã¯åœã§ãã
>>> SomeClass.classm
<bound method SomeClass.classm of <class '__main__.SomeClass'>>
- ã¡ãœãããªããžã§ã¯ãã¯ãé¢æ°ãçããããã€ã³ãããããªããžã§ã¯ããåãã§ããå Žåã«çãããªããŸãããããã£ãŠã
o1.method == o1.method
ã¯çã§ãããã¡ã¢ãªäžã§åããªããžã§ã¯ãã§ã¯ãããŸããã staticmethod
ã¯é¢æ°ããã®ãŸãŸè¿ããããŒãªãã¬ãŒã·ã§ã³ãã®ãã£ã¹ã¯ãªãã¿ã«å€æããŸããã¡ãœãããªããžã§ã¯ãã¯äžåäœæãããªãã®ã§ãis
ã§ã®æ¯èŒã¯çãšãªããŸãã
>>> o1.staticm
<function SomeClass.staticm at ...>
>>> SomeClass.staticm
<function SomeClass.staticm at ...>
- ã€ã³ã¹ã¿ã³ã¹ã¡ãœãããåŒã³åºããã³ã«æ°ãããã¡ãœããããªããžã§ã¯ããäœæãã
self
ãæ¿å ¥ããããã«æ¯ååŒæ°ãå€æŽããå¿ èŠããããããããã©ãŒãã³ã¹ã«æªåœ±é¿ãåãŒããŸããã
CPython 3.7ã¯ãäžæçãªã¡ãœãããªããžã§ã¯ããäœæããã«ã¡ãœãããåŒã³åºãæ°ãããªãã³ãŒããå°å ¥ããããšã§ãããã解決ããŸãããããã¯ã¢ã¯ã»ã¹ãããé¢æ°ãå®éã«åŒã³åºããããšãã®ã¿äœ¿çšãããã®ã§ããã®äŸã®ã¹ããããã¯åœ±é¿ãåãããäŸç¶ãšããŠã¡ãœãããçæããŸã :)
ⶠãã¹ãŠçã§ãã
>>> all([True, True, True])
True
>>> all([True, True, False])
False
>>> all([])
True
>>> all([[]])
False
>>> all([[[]]])
True
ãã®True-Falseã¯ãªã亀äºã«ãªãã®ã§ããããïŒ
ð¡ 解説:
-
all
é¢æ°ã®å®è£ ã¯ä»¥äžã®ããã«ç䟡ã§ããdef all(iterable): for element in iterable: if not element: return False return True
-
all([])
ã¯iterableã空ã§ããããTrue
ãè¿ããŸãã -
all([[]])
ã¯False
ãè¿ããŸãããªããªãæž¡ãããé åã«ã¯[]
ãšããäžã€ã®èŠçŽ ããããPythonã§ã¯ç©ºã®ãªã¹ãã¯åœãšè©äŸ¡ãããããã§ãã -
all([[[]]])
ãšãã以äžã®ååž°çãªããªã¢ã³ãã¯åžžã«True
ã§ããããã¯æž¡ãããé åã®åäžã®èŠçŽ ïŒ[[...]]
ïŒããã¯ã空ã§ã¯ãªããå€ããããªã¹ãã¯çãšè©äŸ¡ãããããã§ãã
ⶠé©ãã®ã³ã³ã
åºå (< 3.6):
>>> def f(x, y,):
... print(x, y)
...
>>> def g(x=4, y=5,):
... print(x, y)
...
>>> def h(x, **kwargs,):
File "<stdin>", line 1
def h(x, **kwargs,):
^
SyntaxError: invalid syntax
>>> def h(*args,):
File "<stdin>", line 1
def h(*args,):
^
SyntaxError: invalid syntax
ð¡ 説æ
- ãã¬ãŒãªã³ã°ã³ã³ãïŒæ«å°Ÿã®ã³ã³ãïŒã¯ãPythoné¢æ°ã®åœ¢åŒãã©ã¡ãŒã¿ãªã¹ãã«ãããŠåžžã«äœ¿ããããã§ã¯ãããŸããã
- Pythonã§ã¯ãåŒæ°ãªã¹ãã¯å è¡ããã³ã³ããšåŸç¶ããã³ã³ãã®äžéšã§å®çŸ©ãããŠããŸãããã®å¯Ÿç«ãåå ã§ãã³ã³ããäžéã«æãŸããã©ã®ã«ãŒã«ã«ãé©åããªãç¶æ³ãçããŸãã
- 泚æ: ãã¬ãŒãªã³ã°ã³ã³ãã®åé¡ã¯Python 3.6ã§ä¿®æ£ãããŠããŸãããã®æçš¿ã®ã³ã¡ã³ãæ¬ã§Pythonã«ããããã¬ãŒãªã³ã°ã³ã³ãã®ããŸããŸãªäœ¿çšæ³ã«ã€ããŠãç°¡åãªè°è«ããããŠããŸãã
ⶠæååãšããã¯ã¹ã©ãã·ã¥
åºå:
>>> print("\"")
"
>>> print(r"\"")
\"
>>> print(r"\")
File "<stdin>", line 1
print(r"\")
^
SyntaxError: æååãªãã©ã«äžã§ã®è¡æ«
>>> r'\'' == "\\'"
True
ð¡ 解説:
-
éåžžã®Pythonæååã§ã¯ãããã¯ã¹ã©ãã·ã¥ã¯ç¹å¥ãªæå³ãæã€æåïŒã·ã³ã°ã«ã¯ã©ãŒããããã«ã¯ã©ãŒããããã¯ã¹ã©ãã·ã¥èªäœãªã©ïŒããšã¹ã±ãŒãããããã«äœ¿çšãããŸãã
>>> "wt\"f" 'wt"f'
ããã¯ã¹ã©ãã·ã¥ã«ç¶ãæåããšã¹ã±ãŒããããšããåäœãšãšãã«ãããã¯ã¹ã©ãã·ã¥èªäœããã®ãŸãŸæž¡ãããŸãã
>>> r'wt\"f' == 'wt\\"f'
True
>>> print(repr(r'wt\"f')
'wt\\"f'
>>> print("\n")
>>> print(r"\\n")
'\\n'
- ããŒãµãŒãçã®æååãªãã©ã«ïŒ
r
ãã¬ãã£ãã¯ã¹ã§ç€ºãããïŒã§ããã¯ã¹ã©ãã·ã¥ïŒ\
ïŒã«ééãããšãäœããã®æåãç¶ããããšãæåŸ ããŸãããããŠããã®ã±ãŒã¹ïŒprint(r"\")
ïŒã§ã¯ãããã¯ã¹ã©ãã·ã¥ãæ«å°Ÿã®åŒçšç¬Šããšã¹ã±ãŒãããããŒãµãŒãçµäºåŒçšç¬Šãªãã«ããŠããŸã£ããããSyntaxError
ãçºçããŠããŸãããã®ãããrawæååã®æ«å°Ÿã«ããã¯ã¹ã©ãã·ã¥ã䜿çšããããšã¯ã§ããŸããã
ⶠnotã®çµã³ç®
x = True
y = False
åºå:
>>> not x == y
True
>>> x == not y
File "<input>", line 1
x == not y
^
SyntaxError: invalid syntax
ð¡ 解説:
- æŒç®åã®åªå
é äœã¯åŒãã©ã®ããã«è©äŸ¡ããããã«åœ±é¿ãäžããŸããPythonã§ã¯ã
==
æŒç®åã¯not
æŒç®åãããåªå é äœãé«ãã§ãã - ãããã£ãŠ
not x == y
ã¯not (x == y)
ãšåçã§ãããããã¯æçµçã«not (True == False)
ãšè©äŸ¡ããTrue
ã«ãªããŸãã - ããã
x == not y
ã¯SyntaxError
ãåŒãèµ·ãããŸããããã¯(x == not) y
ãšåçã§ãããæåã«äºæ³ãããx == (not y)
ã§ã¯ãªãããã§ãã - ããŒãµãŒã¯
not
ããŒã¯ã³ãnot in
æŒç®åã®äžéšãšããŠæåŸ ããŠããŸããïŒãªããªã==
ãšnot in
æŒç®åã¯åãåªå é äœãæã£ãŠããããïŒãããããnot
ããŒã¯ã³ã®åŸã«in
ããŒã¯ã³ãèŠã€ããããªãã£ããããSyntaxError
ãçºçãããŸããã
ⶠååã®ããªãã«ã¯ã©ãŒãæåå
åºå:
>>> print('wtfpython''')
wtfpython
>>> print("wtfpython""")
wtfpython
>>> # 以äžã®ã¹ããŒãã¡ã³ã㯠`SyntaxError` ãçºçããã
>>> # print('''wtfpython')
>>> # print("""wtfpython")
File "<input>", line 3
print("""wtfpython")
^
SyntaxError: EOF while scanning triple-quoted string literal
ð¡ 解説:
-
Pythonã¯æé»çãªæååãªãã©ã«ã®é£çµããµããŒãããŠããŸããäŸãã°ã
>>> print("wtf" "python") wtfpython >>> print("wtf" "") # ãŸã㯠"wtf""" wtf
-
'''
ãš"""
ãPythonã§æåååºåããšããŠäœ¿çšããããããPythonã€ã³ã¿ããªã¿ã¯çŸåšåºäŒã£ãŠããããªãã«ã¯ã©ãŒãæååãªãã©ã«ã®çµãããšããŠããªãã«ã¯ã©ãŒãã®åºåããæåŸ ããŠãããããSyntaxError
ãçºçããŸãã
ⶠããŒã«å€ã®æå€ãªæå
# iterableã«å«ãŸããããŒã«å€ãšæŽæ°ã®æ°ãæ°ããã·ã³ãã«ãªäŸã
mixed_list = [False, 1.0, "some_string", 3, True, [], False]
integers_found_so_far = 0
booleans_found_so_far = 0
for item in mixed_list:
if isinstance(item, int):
integers_found_so_far += 1
elif isinstance(item, bool):
booleans_found_so_far += 1
åºå:
>>> integers_found_so_far
4
>>> booleans_found_so_far
0
>>> some_bool = True
>>> "wtf" * some_bool
'wtf'
>>> some_bool = False
>>> "wtf" * some_bool
''
def tell_truth():
True = False
if True == False:
print("I have lost faith in truth!")
åºå (< 3.x):
>>> tell_truth()
I have lost faith in truth!
ð¡ 説æ:
-
Pythonã«ããã
bool
ã¯int
ã®ãµãã¯ã©ã¹ã§ã>>> issubclass(bool, int) True >>> issubclass(int, bool) False
ãã®ãããTrue
ãš False
㯠int
ã®ã€ã³ã¹ã¿ã³ã¹ã§ãã
>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
True
ã®æŽæ°å€ã¯ 1
ã§ãFalse
ã®æŽæ°å€ã¯ 0
ã§ãã
>>> int(True)
1
>>> int(False)
0
- 詳现ãªçç±ã«ã€ããŠã¯ããã®StackOverflowã®åçããåç §ãã ããã
- å
ã
Pythonã«ã¯
bool
åããªãïŒåœãè¡šãã«ã¯ 0 ããçãè¡šãã«ã¯ 1 ã®ãããªéãŒãå€ã䜿çšããŠããŸããïŒãTrue
ãFalse
ããããŠbool
åã 2.x ããŒãžã§ã³ã§è¿œå ãããŸããããåŸæ¹äºææ§ã®ããã«True
ãšFalse
ãå®æ°ã«ããããšã¯ã§ããŸããã§ããããããã¯åãªããã«ãã€ã³å€æ°ã§ãåå²ãåœãŠãå¯èœã§ããã - Python 3 ã¯åŸæ¹äºææ§ããªãã£ãã®ã§ãã€ãã«ãã®åé¡ãä¿®æ£ããããããã£ãŠæåŸã®ã¹ãããã㯠Python 3.x ã§ã¯åäœããŸããïŒ
ⶠã¯ã©ã¹å±æ§ãšã€ã³ã¹ã¿ã³ã¹å±æ§
class A:
x = 1
class B(A):
pass
class C(A):
pass
åºå:
>>> A.x, B.x, C.x
(1, 1, 1)
>>> B.x = 2
>>> A.x, B.x, C.x
(1, 2, 1)
>>> A.x = 3
>>> A.x, B.x, C.x # C.xã¯å€ãã£ãããB.xã¯å€ãããªãã£ã
(3, 2, 3)
>>> a = A()
>>> a.x, A.x
(3, 3)
>>> a.x += 1
>>> a.x, A.x
(4, 3)
class SomeClass:
some_var = 15
some_list = [5]
another_list = [5]
def __init__(self, x):
self.some_var = x + 1
self.some_list = self.some_list + [x]
self.another_list += [x]
åºå:
>>> some_obj = SomeClass(420)
>>> some_obj.some_list
[5, 420]
>>> some_obj.another_list
[5, 420]
>>> another_obj = SomeClass(111)
>>> another_obj.some_list
[5, 111]
>>> another_obj.another_list
[5, 420, 111]
>>> another_obj.another_list is SomeClass.another_list
True
>>> another_obj.another_list is some_obj.another_list
True
ð¡ 解説
- ã¯ã©ã¹å€æ°ãšã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ã®å€æ°ã¯ãã¯ã©ã¹ãªããžã§ã¯ãã®èŸæžãšããŠå éšçã«æ±ãããŸããå€æ°åãçŸåšã®ã¯ã©ã¹ã®èŸæžã«èŠã€ãããªãå Žåã¯ã芪ã¯ã©ã¹ãæ€çŽ¢ãããŸãã
+=
æŒç®åã¯ãæ°ãããªããžã§ã¯ããäœæããã«å€æŽå¯èœãªãªããžã§ã¯ãããã®å Žã§å€æŽããŸãããããã£ãŠãããã€ã³ã¹ã¿ã³ã¹ã®å±æ§ãå€æŽãããšãä»ã®ã€ã³ã¹ã¿ã³ã¹ãšã¯ã©ã¹å±æ§ã«ã圱é¿ãåã³ãŸãã
ⶠNoneã®çæ
some_iterable = ('a', 'b')
def some_func(val):
return "something
åºå (<= 3.7.x):
>>> [x for x in some_iterable]
['a', 'b']
>>> [(yield x) for x in some_iterable]
<generator object <listcomp> at 0x7f70b0a4ad58>
>>> list([(yield x) for x in some_iterable])
['a', 'b']
>>> list((yield x) for x in some_iterable)
['a', None, 'b', None]
>>> list(some_func((yield x)) for x in some_iterable)
['a', 'something', 'b', 'something']
ð¡ 解説:
- ããã¯ããžã§ãã¬ãŒã¿ãšå
æ¬çãªç解ã§ã®
yield
ã®CPythonã®æ±ãã«é¢ãããã°ã§ãã - ãœãŒã¹ãšè©³ãã解説ã¯ãã¡ã
- é¢é£ãããã°ã¬ããŒã: https://bugs.python.org/issue10544
- Python 3.8+ ã§ã¯ããªã¹ãå
å
è¡šèšã§
yield
ã䜿çšããããšã¯èš±å¯ãããŠããããSyntaxError
ãæããããŸãã
ⶠreturnããyield
def some_func(x):
if x == 3:
return ["wtf"]
else:
yield from range(x)
åºå (> 3.3):
>>> list(some_func(3))
[]
"wtf"
ã¯ã©ãã«æ¶ããã®ã§ããããïŒããã¯yield from
ã®ç¹å¥ãªå¹æã«ãããã®ã§ããããïŒæ€èšŒããŠã¿ãŸãããã
def some_func(x):
if x == 3:
return ["wtf"]
else:
for i in range(x):
yield i
åºå:
>>> list(some_func(3))
[]
åãçµæã§ããæ©èœããŸããã§ããã
ð¡ 解説:
- Python 3.3以éããžã§ãã¬ãŒã¿å
ã§å€ã䌎ã
return
ã¹ããŒãã¡ã³ãã䜿çšããããšãå¯èœã«ãªããŸããïŒPEP380ãåç §ïŒãå ¬åŒããã¥ã¡ã³ãã§ã¯ã
"... ãžã§ãã¬ãŒã¿ã®äžã§return exprã䜿çšãããšããžã§ãã¬ãŒã¿ã®çµäºæã«StopIteration(expr)ãçºçããã"
ãšãããŸãã
some_func(3)
ã®ã±ãŒã¹ã§ã¯ãreturn
ã¹ããŒãã¡ã³ãã®ããã«StopIteration
ãçºçããæåã«ãªã¹ãã空ã«ãªããŸããStopIteration
äŸå€ã¯ãlist(...)
ã©ãããŒãfor
ã«ãŒãå ã§èªåçã«ãã£ãããããŸãããããã£ãŠãäžèšã®2ã€ã®ã¹ããããã¯ç©ºã®ãªã¹ãã«ãªããŸãã- ãžã§ãã¬ãŒã¿
some_func
ãã["wtf"]
ãååŸããã«ã¯ãStopIteration
äŸå€ããã£ããããå¿ èŠããããŸãã
try:
next(some_func(3))
except StopIteration as e:
some_string = e.valu
>>> some_string ["wtf"]
ⶠNanã®ååž°æ§
a = float('inf')
b = float('nan')
c = float('-iNf') # 倧æåå°æåã¯åºå¥ããŸãã
d = float('nan')
åºå:
>>> a
inf
>>> b
nan
>>> c
-inf
>>> float('some_other_string')
ValueError: could not convert string to float: some_other_string
>>> a == -c # inf==inf
True
>>> None == None # None == None
True
>>> b == d # but nan!=nan
False
>>> 50 / a
0.0
>>> a / a
nan
>>> 23 + b
nan
>>> x = float('nan')
>>> y = x / x
>>> y is y # åäžæ§ã¯ä¿æ
True
>>> y == y # yã®ç䟡æ§ã¯å€±æ
False
>>> [y] == [y] # ã ãyãå«ããªã¹ãã®ç䟡æ§ã¯æå
True
ð¡ 解説:
'inf'
ãš'nan'
ã¯ç¹å¥ãªæååïŒå€§æåå°æåãåºå¥ããªãïŒã§ãæ瀺çã«float
åã«ãã£ã¹ããããšãããããæ°åŠçãª"ç¡é"ãš"éæ°"ãè¡šãããã«äœ¿çšãããŸãã- IEEEæšæºã«ãããš
NaN != NaN
ãªã®ã§ããã®ã«ãŒã«ã«åŸããšãPythonã®ã³ã¬ã¯ã·ã§ã³èŠçŽ ã®åå°æ§ã®ä»®å®ãç ŽãããŸããã€ãŸããx
ãlist
ã®ãããªã³ã¬ã¯ã·ã§ã³ã®äžéšã§ããå Žåãæ¯èŒã®ãããªå®è£ ã¯x == x
ãšããä»®å®ã«åºã¥ããŠããŸãããã®ä»®å®ã«ããããŸãåäžæ§ãæ¯èŒãããŸãïŒé床ãéãããïŒãå€ã¯åäžæ§ãäžäžèŽã®å Žåã«ã®ã¿æ¯èŒãããŸãã以äžã®ã¹ããããã§ããæ確ã«ç解ã§ããã§ãããã
>>> x = float('nan')
>>> x == x, [x] == [x]
(False, True)
>>> y = float('nan')
>>> y == y, [y] == [y]
(False, True)
>>> x == y, [x] == [y]
(False, False
x
ãšy
ã®åäžæ§ãç°ãªããããå€ãèæ ®ãããããããç°ãªããããæ¯èŒã¯ä»åFalse
ãè¿ããŸãã- åè: Reflexivity, and other pillars of civilization
ⶠäžå€ã®å€ç°
Pythonã®åç §ã®ä»çµã¿ãç¥ã£ãŠããã°ãããã¯äºçŽ°ãªããšãããããŸããã
some_tuple = ("A", "tuple", "with", "values")
another_tuple = ([1, 2], [3, 4], [5, 6])
åºå:
>>> some_tuple[2] = "change this"
TypeError: 'tuple' object does not support item assignment
>>> another_tuple[2].append(1000) #ããã¯ãšã©ãŒãæããªã
>>> another_tuple
([1, 2], [3, 4], [5, 6, 1000])
>>> another_tuple[2] += [99, 999]
TypeError: 'tuple' object does not support item assignment
>>> another_tuple
([1, 2], [3, 4], [5, 6, 1000, 99, 999])
ããïŒtupleã¯äžå€ã ã£ãã®ã§ã¯ïŒ
ð¡ 解説:
-
https://docs.python.org/3/reference/datamodel.html ããã®åŒçš
äžå€ã®ã·ãŒã±ã³ã¹
äžå€ã®ã·ãŒã±ã³ã¹åã®ãªããžã§ã¯ãã¯ãäžåºŠäœæããããšå€æŽããããšã¯ã§ããŸãããïŒãªããžã§ã¯ããä»ã®ãªããžã§ã¯ããžã®åç §ãå«ãã§ããå Žåããããã®ä»ã®ãªããžã§ã¯ãã¯å€æŽå¯èœã§ããå€æŽãããããšããããŸããããããäžå€ãªããžã§ã¯ããçŽæ¥åç §ãããªããžã§ã¯ãã®éåã¯å€æŽããããšãã§ããŸãããïŒ -
+=
æŒç®åã¯æ°ãããªããžã§ã¯ããäœæããã«ãå€æŽå¯èœãªãªããžã§ã¯ãããã®å Žã§å€æŽããŸããã¢ã€ãã ã®ä»£å ¥ã¯æ©èœããŸããããäŸå€ãçºçãããšãã«ã¯ãã¢ã€ãã ã¯ãã§ã«ãã®å Žã§å€æŽãããŠããŸãã -
Pythonã®å ¬åŒ FAQã«ã説æããããŸãã
ⶠå€éšã¹ã³ãŒãããæ¶ããå€æ°
e = 7
try:
raise Exception()
except Exception as e:
pass
åºå (Python 2.x):
>>> print(e)
# äœã衚瀺ãããªã
åºå (Python 3.x):
>>> print(e)
NameError: name 'e' is not defined
ð¡ 解説:
-
åºå ž: https://docs.python.org/3/reference/compound_stmts.html#except
as
ã¿ãŒã²ããã䜿ã£ãŠäŸå€ãå²ãåœãŠãããå Žåãexcept
ç¯ã®çµããã§ã¯ãªã¢ãããŸãããŸãã§except E as N: foo
ã以äžã«å€æããããã®ããã§ãã
except E as N:
try:
foo
finally:
del N
- ããã¯ãäŸå€ã
except
ç¯ã®åŸã§ãåç §ããããã«ã¯ãäŸå€ãå¥ã®ååã«å²ãåœãŠãå¿ èŠãããããšãæå³ããŸããäŸå€ã¯ããã¬ãŒã¹ããã¯ãæ·»ä»ãããŠãããããã¹ã¿ãã¯ãã¬ãŒã ãšåç §ãµã€ã¯ã«ã圢æãã次ã®ã¬ããŒãžã³ã¬ã¯ã·ã§ã³ãè¡ããããŸã§ãã®ãã¬ãŒã å ã®ãã¹ãŠã®ããŒã«ã«å€æ°ãçãç¶ããããŸãã - Pythonã§ã¯ç¯ã«ã¹ã³ãŒãããããŸãããäŸã®äžã®å
šãŠãåãã¹ã³ãŒãå
ã«ååšããå€æ°
e
ã¯except
ç¯ã®å®è¡ã«ãã£ãŠé€å»ãããŸãããããã¯ãããããã®å éšã¹ã³ãŒããæã€é¢æ°ã§ã¯åœãŠã¯ãŸããŸããã以äžã®äŸããããåãããããè¡šããŠããŸãïŒ
def f(x):
del(x)
print(x)
x = 5
y = [5, 4, 3]
åºå:
>>> f(x)
UnboundLocalError: local variable 'x' referenced before assignment
>>> f(y)
UnboundLocalError: local variable 'x' referenced before assignment
>>> x
5
>>> y
[5, 4, 3]
Python 2.xã§ã¯ãå€æ°åe
ã¯Exception()
ã€ã³ã¹ã¿ã³ã¹ã«å²ãåœãŠãããã®ã§ãprint
ããããšããŠãäœã衚瀺ãããŸããã
åºå (Python 2.x):
>>> e
Exception()
>>> print e
# äœã衚瀺ãããªãïŒ
ⶠè¬ã®ããŒåå€æ
class SomeClass(str):
pass
some_dict = {'s': 42}
åºå:
>>> type(list(some_dict.keys())[0])
str
>>> s = SomeClass('s')
>>> some_dict[s] = 40>>> some_dict # 2ã€ã®ç°ãªãããŒå€ãã¢ãæåŸ
ããã
{'s': 40}
>>> type(list(some_dict.keys())[0])
str
ð¡ 解説:
-
ãªããžã§ã¯ã
s
ãšæåå"s"
ã¯åãå€ã«ããã·ã¥ãããŸãããªããªãSomeClass
ã¯str
ã¯ã©ã¹ã®__hash__
ã¡ãœãããç¶æ¿ããŠããããã§ãã -
SomeClass("s") == "s"
ã¯True
ã«è©äŸ¡ãããŸãããªããªãSomeClass
ã¯str
ã¯ã©ã¹ãã__eq__
ã¡ãœãããç¶æ¿ããŠããããã§ãã -
äž¡æ¹ã®ãªããžã§ã¯ããåãå€ã«ããã·ã¥ãããçãããããèŸæžå ã§åãããŒã§è¡šãããŸãã
-
æãŸããåäœãåŸãããã«ã¯ã
SomeClass
å ã§__eq__
ã¡ãœãããåå®çŸ©ããããšãã§ããŸããclass SomeClass(str): def __eq__(self, other): return ( type(self) is SomeClass and type(other) is SomeClass and super().__eq__(other) ) # ã«ã¹ã¿ã ã® __eq__ ãå®çŸ©ãããšãPythonã¯èªåçã« __hash__ ã¡ãœãããç¶æ¿ããªããªãããã # ãããå®çŸ©ããå¿ èŠããã __hash__ = str.__hash__ some_dict = {'s':42}
åºå:
>>> s = SomeClass('s')
>>> some_dict[s] = 40>>> some_dict
{'s': 40, 's': 42}
>>> keys = list(some_dict.keys())
>>> type(keys[0]), type(keys[1])
(__main__.SomeClass, str)
ⶠããã¯åããããªïŒ
a, b = a[b] = {}, 5
åºå:
>>> a
{5: ({...}, 5)}
ð¡ 解説
Python language referenceã«ãããšã代å
¥æã¯ä»¥äžã®åœ¢ããšããŸãã
```python
(target_list "=")+ (expression_list | yield_expression)
```
ãããŠä»¥äžã®ããã«ãªããŸãã
-
ä»£å ¥æã¯åŒãªã¹ããè©äŸ¡ãïŒããã¯åäžã®åŒããã«ã³ãã§åºåããããªã¹ãã§ãããåŸè ã¯ã¿ãã«ãçæããŸãïŒãåŸãããåäžã®ãªããžã§ã¯ãã¯å·Šããå³ã«åã¿ãŒã²ãããªã¹ãã«å²ãåœãŠãããŸãã
-
(target_list "=")+
ã®+
㯠1ã€ä»¥äžã® ã¿ãŒã²ãããªã¹ããããããšãæå³ããŸãããã®å Žåã®ã¿ãŒã²ãããªã¹ãã¯a, b
ãša[b]
ã§ãïŒåŒãªã¹ãã¯1ã€ã ãã§ããã®å Žåã¯{}, 5
ã§ãïŒã -
åŒãªã¹ããè©äŸ¡ãããåŸããã®å€ã¯ å·Šããå³ ãžã¿ãŒã²ãããªã¹ãã«ã¢ã³ããã¯ãããŸãããããã£ãŠããã®ã±ãŒã¹ã§ã¯ãæåã«
{}, 5
ã®ã¿ãã«ãa, b
ã«ã¢ã³ããã¯ãããa = {}
ãšb = 5
ã«ãªããŸãã -
a
ã¯{}
ã«å²ãåœãŠãããŸããããã¯å€æŽå¯èœãªãªããžã§ã¯ãã§ãã -
2ã€ç®ã®ã¿ãŒã²ãããªã¹ãã¯
a[b]
ã§ãïŒãã®æç¹ã§a
ãšb
ãåã®æã§å®çŸ©ãããŠããªãããããšã©ãŒãçºçãããšèãããããããŸããããèŠããŠãããŠãã ãããç§ãã¡ã¯ã¡ããã©a
ã{}
ã«ãb
ã5
ã«å²ãåœãŠãŸããïŒã -
ããã§ãèŸæžã®ããŒ
5
ã«ã¿ãã«({}, 5)
ãèšå®ãã埪ç°åç §ãäœæããŸãïŒåºåã®{...}
ã¯a
ãæ¢ã«åç §ããŠããåããªããžã§ã¯ããæããŸãïŒã埪ç°åç §ã®å¥ã®ç°¡åãªäŸã¯ä»¥äžã®éãã§ãã -
ã®éãã§ãã
>>> some_list = some_list[0] = [0] >>> some_list [[...]] >>> some_list[0] [[...]] >>> some_list is some_list[0] True >>> some_list[0][0][0][0][0][0] == some_list True
-
ãã®äŸã§ãåæ§ã§ãïŒ
a[b][0]
ã¯a
ãšåããªããžã§ã¯ãã§ãïŒã -
ãŸãšãããšããã®ããã«åã¿ç ãããšãã§ããŸãã
a, b = {}, 5 a[b] = a, b
ãããŠã埪ç°åç
§ã¯ a[b][0]
ã a
ãšåããªããžã§ã¯ãã§ãããšããäºå®ã«ãã£ãŠæ£åœåãããŸãã
>>> a[b][0] is a
True
ⶠæŽæ°æååå€æãéçãè¶ ãããšã
>>> # Python 3.10.6
>>> int("2" * 5432)
>>> # Python 3.10.8
>>> int("2" * 5432)
åºå:
>>> # Python 3.10.6
222222222222222222222222222222222222222222222222222222222222222...
>>> # Python 3.10.8
Traceback (most recent call last):
...
ValueError: Exceeds the limit (4300) for integer string conversion:
value has 5432 digits; use sys.set_int_max_str_digits()
to increase the limit.
ð¡ 解説:
ãã® int()
ã®åŒã³åºãã¯Python 3.10.6ã§ã¯åé¡ãªãåäœããPython 3.10.8ã§ã¯ ValueError
ãåŒãèµ·ãããŸããPythonã¯äŸç¶ãšããŠå€§ããªæŽæ°ãæ±ãããšãã§ããŸããããã®ãšã©ãŒã¯æŽæ°ãšæååã®éã§å€æãããšãã«ã®ã¿çºçããŸãã
幞ãã«ããäºæ³ãããæäœããã®éçãè¶ ããå Žåãèš±å¯ãããæ¡æ°ã®éçãå¢ããããšãã§ããŸãããããè¡ãããã«ã以äžã®ããããã䜿çšã§ããŸãïŒ
- ã³ãã³ãã©ã€ã³ãã©ã°
X int_max_str_digits
sys
ã¢ãžã¥ãŒã«ããã®set_int_max_str_digits()
é¢æ°- ç°å¢å€æ°
PYTHONINTMAXSTRDIGITS
ãã®å€ãè¶ ããããšãäºæ³ãããã³ãŒãã«ã€ããŠãããã©ã«ãã®éçãå€æŽãã詳现ã«ã€ããŠã¯ããã¡ããåç §ã