PythonæµïŒðããã°ã©ãã³ã°ã¹ã¿ã€ã«å¯Ÿæ±ºïŒð ãã£ã¬ã³ãžã£ãŒæŽŸ vs æ é掟 ãããªãã¯ã©ã£ã¡ïŒã
ããã«ã¡ã¯ïŒãšã³ãžãã¢ã®ããã£ã¡ã§ãïŒðž
ä»æ¥ã¯ãããã°ã©ãã³ã°ã®äžçã§ããèŠãããã2ã€ã®çãæ¹ãã«ã€ããŠãã話ãããããšæããŸããå®ã¯ãããã£ãŠæ®æ®µã®ç掻ã«ããããåœãŠã¯ãŸããã§ãïŒ
é£ããèšããšãäž¡è ã¯ãEAFPãããLBYLããšè±èªã®ç¥èªã§èšããããããŸãããèŠãã«ããã®ã§åã¯ããã£ã¬ã³ãžã£ãŒæŽŸããšãæ é掟ããšåŒãã§ããŸãïŒ
EAFPã£ãŠäœïŒð€
EAFP (Easier to Ask for Forgiveness than Permission) ã¯ãçŽèš³ãããšãèš±å¯ãæ±ããããè¬çœªããæ¹ãç°¡åããšããæå³ã§ãããªãã ãã€ã³ãã£ãªæããããŸãããïŒç¬ïŒ
ç°¡åã«èšããšãããšã©ãŒãèµ·ããããïŒãŸããã£ãŠã¿ããïŒãšã©ãŒãåºãã察åŠããã°ãããããïŒããšããã¹ã¿ã€ã«ã§ãã
æ éã«ç¢ºèªæŽŸ (LBYL)ð
ããã«å¯ŸããŠãLook Before You Leapãã¯ãçŽèš³ãããšãé£ã³èŸŒãåã«ããèŠãããšããæå³ã§ãã
ã転ã°ã¬å
ã®æðªããã¢ãããŒã«ãå®è¡åã«å¿
ããã§ãã¯ãå
¥ããŸãð
ãæ¯ããã®æããããªãæ
é掟ã®èãæ¹ã§ãã
ð¯ 身è¿ãªå Žé¢ã§èŠãããã£ã¬ã³ãžã£ãŒæŽŸ vs æ é掟ã
å®ã¯ãã®éããç§ãã¡ã®èº«è¿ãªãšããã§ãããããªèŠããããã§ãïŒå ·äœçã«èŠãŠã¿ããš...
äŸãã°ããããªå Žé¢...
ð» 飲ã¿äŒã®å¹¹äºã«ãªã£ããšã
ã»ãã£ã¬ã³ãžã£ãŒæŽŸïŒããšããããã人æ°åºãäºçŽãã¡ãããïŒäººæ°ã¯åŸã§èª¿æŽããã°äœãšããªãã§ããïŒã
ã»æ
é掟ïŒããŸãã¯LINEã§åå å¯èœäººæ°ã確èªããŠãäºç®ã¢ã³ã±ãŒããåã£ãŠãåè£åºããªã¹ãã¢ããããŠ...ã
ð® æ°ããã²ãŒã ãå§ãããšã
ã»ãã£ã¬ã³ãžã£ãŒæŽŸïŒããã¥ãŒããªã¢ã«ïŒé£ã°ãã¡ããïŒå®è·µã§èŠããã®ãäžçªæ©ãããïŒã
ã»æ
é掟ïŒããŸãã¯æäœæ¹æ³ããã£ãã確èªããŠãæ»ç¥ãµã€ããèªãã§ãã...ã
ð éåãå§ãããšã
ã»ãã£ã¬ã³ãžã£ãŒæŽŸïŒããããææ¥ãããžã éãå§ãããïŒãŸãã¯å
¥äŒãã¡ããïŒã
ã»æ
é掟ïŒãäœå枬å®ããŠãããè¿æã®ãžã ãå
šéšæ¯èŒããŠãç¡æäœéšãåããŠãã...ã
ð³ æ°ããã¬ã·ãã«ææŠãããšã
ã»ãã£ã¬ã³ãžã£ãŒæŽŸïŒããªããšãªãäœãæ¹ããã£ãããã¢ã¬ã³ãžãå ããªããäœã£ãŠã¿ããïŒã
ã»æ
é掟ïŒãã¬ã·ãã3åèªãã§ãåéãèšã£ãŠãæé ã確èªããŠãã...ã
ð± æ°ããã¢ããªã®äœ¿ãæ¹
ã»ãã£ã¬ã³ãžã£ãŒæŽŸïŒããšãããããããã觊ã£ãŠã¿ãã°äœ¿ãæ¹ãªããŠåããã§ããïŒã
ã»æ
é掟ïŒããŸãã¯ãã«ããèªãã§ãåºæ¬æ©èœãç解ããŠãã䜿ãå§ããã...ã
âïž æ
è¡ã®èšç»
ã»ãã£ã¬ã³ãžã£ãŒæŽŸïŒããŸãã¯èªç©ºåžåã£ã¡ããïŒçŸå°ã®ããšã¯çããŠããèãããïŒã
ã»æ
é掟ïŒããŸãã¯å€©æ°äºå ±ãã§ãã¯ããŠãçŸå°ã®æ²»å®èª¿ã¹ãŠãå£ã³ããµã€ããèŠãŠ...ã
ããã°ã©ãã³ã°ã®2倧ã¹ã¿ã€ã«ãã³ãŒã察決ïŒð¥
ããããã³ãŒããèŠãããªã£ãŠããŸããïŒð
ããã§ã¯ãäž¡è
ã®ã³ãŒããèŠæ¯ã¹ãŠã¿ãŸãããã
èŸæžãæ±ããšã ð
ã§ã¯æ©éãæ
é掟ã®ã³ãŒããèŠãŠã¿ãŸãããïŒ
æ³åããŠãã ãã...ã¿ããªãæã£ãŠããã¹ããã®é»è©±åž³ð±ã®ãããªãèŸæžããšããããŒã¿ãæ±ããšã...
# äŸãã°ããããªæãã®äœæé²ããŒã¿ããããšããŸãïŒ
# ãã£ãšãé»è©±çªå·ã幎霢ãšããããŒããªãã§ããïŒ
# ã§ã倧äžå€«ãæ
é掟ããã°ã©ããŒãªããããªããšãæ³å®æžã¿...ð
user_data = {
"name": "ããã£ã¡", # ãååã¯ã¡ãããšããïŒ
"address": "noteçº1äžç®", # äœæãããããªïŒ
"job": "ãšã³ãžãã¢", # ãä»äºãç»é²æžã¿
"hobby": "é³æ¥œ" # 趣å³ãŸã§æžããŠããçŽ æŽãããïŒ
}
ð¢ è¶ æ é掟ããã°ã©ããŒã®å Žå
# ð¢ è¶
æ
é掟ããã°ã©ããŒã®å Žå
# ïŒãã¯ããæ¯ããã¬ãã«ã®å¿é
æ§ïŒïŒ
if user_data is not None: # ãŸãäœæé²ããŒã¿èªäœãããããïŒãããããŠæ¶ããŠãïŒ
if isinstance(user_data, dict): # ãã³ãã«äœæé²åœ¢åŒïŒãªããéã圢åŒã«ãªã£ãŠãªãïŒ
if len(user_data) > 0: # 空ã£ãœãããªãããïŒå
šéšæ¶ããŠãªãïŒ
if "phone" in user_data: # é»è©±çªå·ã¯ããïŒäžå¿ç¢ºèªïŒ
if user_data["phone"] is not None: # é»è©±çªå·ãããªãããããªãïŒ
if isinstance(user_data["phone"], str): # ã¡ãããšæåååïŒæ°åã ããããªãïŒ
if len(user_data["phone"]) > 0: # 空æåãããªãïŒ
phone = user_data["phone"] # ãã£ãšïŒé»è©±çªå·ãgetïŒ
else:
phone = None # 空æåã¯ãã¡ïŒ
else:
phone = None # æååãããªãïŒ
else:
phone = None # ããªããã ã£ã...
else:
phone = None # ãã£ã±ãé»è©±çªå·ããŒããªãïŒäºæ³éãïŒ
else:
phone = None # å®å
šã«ç©ºã£ãœ...
else:
phone = None # äœæé²åœ¢åŒãããªãïŒ
else:
phone = None # ããããããŒã¿ãååšããªãïŒ
ãïŒð
æ
é掟ããã°ã©ããŒã£ãŠããŸãã§ãæ¯ããã¿ããïŒ
ãåæã£ãïŒãåŒåœã¯ïŒãéã¯ïŒæºåž¯ã®å
é»ã¯ïŒãã£ãŠç¢ºèªããæãã§ã ãšã«ããäœéã«ã確èªããã¡ãããã§ãã
ã§ããå®ã¯ãããããã°ã©ãã³ã°ã®äžçã§ã¯ãé²è¡çããã°ã©ãã³ã°ãã£ãŠåŒã°ããŠããŠã 決ããŠæªãããšãããªããã§ãããã ...ã¡ãã£ãšå¿é ãããããïŒð€
ãã®åŸããã£ãšã¯ãŒã«ã§ã¹ããŒããªãææŠæŽŸãã®ããæ¹ã玹ä»ãããŠãã ããïŒ
ðãã£ã¬ã³ãžã£ãŒæŽŸ
ããŠãã次ã¯ãã£ã¬ã³ãžã£ãŒæŽŸã®ç»å Žã§ãïŒ
æ
é掟ãšã¯çéã®ããŸãã¯ãã£ãŠã¿ããïŒã粟ç¥ã®æã¡äž»ã§ã ðââïžðš
# ãã£ã¬ã³ãžã£ãŒã¹ã¿ã€ã«
# æ
é掟ããããªç¡è¬ãª...確èªã足ããªããïŒã
# ãã£ã¬ã³ãžã£ãŒãããããããšãããããã©ã€ïŒã
try:
phone = user_data["phone"] # é»è©±çªå·ååŸïŒããããïŒ
except Exception: # ãšã©ãŒãåºãã...ãã®æèãããïŒ
phone = None # ã¯ããè«Šãã次è¡ããŸããã
EAFPã¹ã¿ã€ã«ã®ã»ããã¹ãããªããŠããŸããïŒ
ãã1ã€äŸãèŠãŠã¿ãŸãããã
ãã¡ã€ã«ãèªããšã ð
ããã°ã©ãã³ã°ã®äžçã§ããã¡ã€ã«ãéãããšããã®ã¯ã ãŸãã§ãæ°ã«å ¥ãã®æ¬ãå³æžé€šã§æ¢ããããªãã®ïŒð
ã§ããããã§ãæ é掟ãšãã£ã¬ã³ãžã£ãŒæŽŸã§å€§ããªéãã...ïŒ
ð¢ è¶ å¿é æ§ã®ãã¡ã€ã«ã¢ã¯ã»ã¹ã³ãŒãïŒãã¯ããã¡ã€ã«ææçïŒ
# ð¢ è¶
å¿é
æ§ã®ãã¡ã€ã«ã¢ã¯ã»ã¹ã³ãŒãïŒãã¯ããã¡ã€ã«ææçïŒ
if file_path is not None: # ãŸããã¹ãNoneãããªãããïŒäžå®...
if isinstance(file_path, str): # æååã ããïŒå¿é
ã ãªã
if len(file_path) > 0: # 空æåãããªãããïŒ
if os.path.exists(file_path): # ãã¡ã€ã«ã¯ååšããïŒãããã...
if os.path.isfile(file_path): # ãã³ãã«ãã¡ã€ã«ïŒãã£ã¬ã¯ããªãããªãïŒ
if os.access(file_path, os.R_OK): # èªã¿åãæš©éããïŒäžå®ã ã...
if os.path.getsize(file_path) > 0: # 空ã®ãã¡ã€ã«ãããªãïŒ
with open(file_path, 'r', encoding='utf-8') as file: # ãã£ãšopenïŒ
if file.readable(): # æ¬åœã«èªããã®ïŒå¿é
...
content = file.read() # ãã£ãšèªãã...ïŒã§ãäžèº«ã¯å€§äžå€«ããªïŒïŒ
else:
print("ãµã€ãºã0ïŒããã...")
else:
print("æš©éããªãïŒã©ãããã...")
else:
print("ãã¡ã€ã«ãããªãïŒãã£ã¬ã¯ããªãã...æã...")
else:
print("ååšããªãïŒäžå®ã§èãçã...")
else:
print("ãã¹ã空æåïŒå¿é
æ§ã§è¯ãã£ã...")
else:
print("æååãããªãïŒãã£ã±ããã§ãã¯ããŠæ£è§£...")
else:
print("ãã¹ãNoneïŒç¢ºèªå€§äº...")
æ
é掟ã¯å³æžé€šã«è¡ãåããå¿é
ãæ¢ãŸããªãïŒ
...ãŸãã§å³æžé€šã«è¡ãåã«ïŒ
ã»å³æžé€šéããŠãããïŒ
ã»å³æžé€šã«ãŒãã¯æã£ãŠãïŒ
ã»é通æéã¯ç¢ºèªããïŒ
ã»ãã®æ¬ãŸã 貞ãåºãäžãããªãïŒ
ã»å³æžé€šèªäœãŸã ååšããŠãïŒ
ã»æ¬æ£ã¯åããŠãªãïŒ
ã»è«ã«é£ãããŠãªãïŒ
ã£ãŠ100åãããå¿é
ããã¿ã€ãïŒð
ð ãã£ã¬ã³ãžã£ãŒæŽŸã®ãã¡ã€ã«ã¢ã¯ã»ã¹ã³ãŒã
ãã£ã¬ã³ãžã£ãŒæŽŸã®å Žå ãæ¬ãèªã¿ãããªãå³æžé€šè¡ãã°ãããããïŒðã
# 察ç
§çãªãã£ã¬ã³ãžã£ãŒæŽŸããŒãžã§ã³
try: # ãã¡ã€ã«èªããïŒããããïŒ
with open(file_path) as f: # ãšããããéããŠã¿ãã
content = f.read() # ãšã©ãŒãåºãã...ãã®æèãããïŒ
except Exception:
raise Exception("ãã¡ã€ã«èªããªãã£ãããåŸã¯ä»»ããïŒð")
EAFPã¹ã¿ã€ã«ã®ã»ããã¹ãããªããŠããŸããïŒ
çµå±ã©ã£ã¡ãè¯ãã®ïŒð€
çµå±ã©ã£ã¡ãããã®ïŒ
äžé·äžçãããã©ãEAFPã®ã»ãããPython颚ãšãããŠãããã§ããæšæºã©ã€ãã©ãªãè²ã
ãªãšããã§ãã®æžãæ¹ãèŠããããã
(åç
§: https://docs.python.org/3/glossary.html#term-EAFP)
ã¡ãªã¿ã«ãåã¯ããã£ã¬ã³ãžã£ãŒæŽŸãã倧奜ãã§ãïŒ ã ã£ãŠèããŠã¿ãŠïŒ
æ
é掟ã®å ŽåïŒ
ã»å¿é
äºã10åç¢ºèª â¡ïž å
šéšOKã§ããã¡ã€ã«ãèªãããšã¯éããªã
ã»ããã«æ°ããå¿é
ãåºãŠãã â¡ïž ããã¡ã€ã«ã®äžèº«ããã€ããªã ã£ããïŒããéäžã§ç ŽæããŠããïŒã
ã»ç¢ºèªé
ç®ãå¢ããããŠå€§å€ïŒð±
å®éèµ·ãããããããã³ã°ãå šéšæ³å®ããã®ã£ãŠé£ããããïŒ
ãã£ã¬ã³ãžã£ãŒæŽŸã®å ŽåïŒ
ã»ãšãããããã£ãŠã¿ã â¡ïž äžæããã£ããã©ãããŒïŒ
ã»ãã¡ã ã£ãã察å¿ãã â¡ïž ãšã©ãŒã¡ãã»ãŒãžã§åå ãåããïŒ
ã»ã³ãŒããã¹ãããªïŒð
ð ãã£ã¬ã³ãžã£ãŒæŽŸã®ãããšãã
- ã³ãŒããã¹ãããªïŒèªã¿ãããïŒ
- æžãã®ã楜ãã³ïŒèããããšãå°ãªãïŒ
- äºæãã¬ãšã©ãŒã«ã察å¿ã§ããïŒ
- Pythonã®ææ³ã«åã£ãŠãïŒïŒPython颚ïŒïŒ
- éçºã¹ããŒããéãïŒ
ã§ããæã«ã¯æ
é掟ã§è¡ãã»ããè¯ããšãããããïŒ
ãšã©ãŒãèµ·ããããšãé »ç¹ãªå Žåãããã®ãšã©ãŒãããèµ·ããããšãç¥ã£ãŠããå Žåã¯æ
é掟ã®ã»ããããã©ãŒãã³ã¹ãè¯ãããšããããã ã
ð æ
é掟ã®ã¡ãªããïŒé©åã«äœ¿ã£ãå ŽåïŒ
- æå³ããªãåäœãé²ããïŒïŒã»ãã¥ãªãã£é¢ã§âïŒ
- åŠçã®æµããåãããããïŒïŒé©åºŠãªifæãªãïŒ
- éèŠãªåææ¡ä»¶ãæ瀺ã§ãã
ã§ãã»ãšãã©ã®å ŽåãèŸæžã®ããŒã«ã¢ã¯ã»ã¹ãããããªå Žåã¯ãæ£åžžãªããŒã¿ãããããšãæ®éã ãããããªãã¢ã¯ã»ã¹ããŠãè¯ããã ã
äŸãã°é»è©±åž³ã®å ŽåãèããŠã¿ãŠ
åéã®é»è©±çªå·ã調ã¹ããšãã«ãããã«é»è©±çªå·ãããããšãæ®éã ãšæã£ãŠé»è©±åž³ãéãããïŒ
ã€ãŸããäžæãããããšã®æ¹ãã»ãšãã©ã ããæ®éãªãã ã
æ¥åžžç掻ã§ããã£ãŠã¿ããæ¡å€äžæãããããšã®æ¹ãæ®éãªããããªãã£ãŠããšïŒ
ãŸãšã ðž
EAFPã¯ãPythonããããã¢ãã³ãªã³ãŒãã£ã³ã°ã¹ã¿ã€ã«ã§ããåãæåã¯æ £ããªããšæããŸããããä»ã§ã¯å€§å¥œããªã¹ã¿ã€ã«ã®1ã€ã§ãïŒ
ã¿ãªãããããã²Pythonãããã³ãŒããæžãããã«ãEAFPã¹ã¿ã€ã«ãåãå ¥ããŠã¿ãŠãã ããïŒ
ããã§ã¯ããŸã次åïŒHappy Coding! âš
ãã®èšäºãæ°ã«å ¥ã£ãããµããŒããããŠã¿ãŸãããïŒ