ã¡ãœããã®è§£æ±ºé åºã«ãããPythonã¯åå«ã¯ã©ã¹ã§çŽæ¥èŠã€ãããªãå Žåãã©ã®ç¥å ã¯ã©ã¹ããã¡ãœãããåŒã³åºãããå€æã§ããŸãã ååå«ã«ç¥å ã1ã€ãããªãå Žåãã¿ã¹ã¯ã¯ç°¡åã§ãã éå±€å šäœã§äžæ¹æ€çŽ¢ãè¡ãããŸãã å€éç¶æ¿ã䜿çšãããŠããå Žåã以äžã«èª¬æããç¹å®ã®åé¡ãçºçããå¯èœæ§ããããŸãã
Pythonã®å€ãããŒãžã§ã³ã§ã¯ãã¡ãœãã解決ã®é åºã¯éåžžã«åå§çã§ãããæ€çŽ¢ã¯ããã¹ãŠã®èŠªã¯ã©ã¹ã§å·Šããå³ãæ倧ã®æ·±ããŸã§å®è¡ãããŸããã ã€ãŸã 次ã«ã芪ã¯ã©ã¹ã«ç®çã®ã¡ãœããããªãã芪ãããå Žåãåãã«ãŒã«ã«åŸã£ãŠæ€çŽ¢ãå®è¡ãããŸãã ããšãã°ã次ã®æ§é ãåããŸãã
AC | | Bd \ / E
ã¯ã©ã¹Eã®ã€ã³ã¹ã¿ã³ã¹ã¡ãœãããåç §ããå Žåããã®ãããªã¢ã«ãŽãªãºã ã¯ã¯ã©ã¹EãBãAãDãããã³Cãé çªã«æ€çŽ¢ããŸãããããã£ãŠãæ€çŽ¢ã¯æåã«æåã®èŠªã¯ã©ã¹ãšãã®ãã¹ãŠã®ç¥å ã§å®è¡ããã次ã«ãã¹ãŠã®ç¥å ãæã€2çªç®ã®èŠªã¯ã©ã¹ã§å®è¡ãããŸãããªã©ããã®ã¡ãœããã¯ãã¯ã©ã¹ãå ±éã®ç¥å ãæã€ãŸã§ç¹å®ã®èŠæ ãåŒãèµ·ãããŸããã§ããã ãã ããããŒãžã§ã³2.2以éã§ã¯ãåºæ¬ã¯ã©ã¹ãªããžã§ã¯ãã衚瀺ãããããããã¹ãŠã®ãŠãŒã¶ãŒã¯ã©ã¹ãç¶æ¿ããããšããå§ãããŸãã ãªãå°å ¥ããããã¯ãå¥ã®èšäºã®ãããã¯ã§ãã ããããæãéèŠãªèŠå ã¯ããªããžã§ã¯ãã¢ãã«ãšã¡ã¿ããŒã¿ã¢ãã«ã®åé¢ã§ãã ããŒãžã§ã³3.0以éãå€ãã¯ã©ã¹ã¯ãµããŒããããªããªãããã¹ãŠã®ãŠãŒã¶ãŒã¯ã©ã¹ã¯ããã©ã«ãã§ãªããžã§ã¯ãã¯ã©ã¹ã«ç±æ¥ããŸãã
ããã«ãããããã€ã€ã¢ã³ãå³ãåé¡ãçºçããŸããã
察象 / \ Ab \ / C
ã¯ã©ã¹Cãç¶æ¿ãããã¯ã©ã¹AãšBãããå Žåãå€ãã¢ã«ãŽãªãºã ïŒCãAããªããžã§ã¯ããBïŒã䜿çšããŠã¡ãœãããæ€çŽ¢ãããšãã¡ãœãããã¯ã©ã¹CãšAã§å®çŸ©ãããŠããªãå Žåããªããžã§ã¯ãããæœåºãããŸãBã§å®çŸ©ãããŠããå Žåãããã«ãããç¹å®ã®äžäŸ¿ãçããŸãã ãªããžã§ã¯ãã¯ã__ init __ã__ str__ãªã©ã®å€ãã®ããžãã¯ã¡ãœãããå®çŸ©ããŸãã ãããããªããžã§ã¯ããç¹å®ã®ãŠãŒã¶ãŒã¯ã©ã¹Dã«çœ®ãæããŠããåé¡ã¯æ®ããŸã-äžäœã¯ã©ã¹ã®ããå ·äœçãªã¡ãœããã®ä»£ããã«ãå ç¥ã¯ã©ã¹ã®ããå ·äœæ§ã®äœãã¡ãœãããæ©èœããŸãã
ãããã£ãŠãã¯ã©ã¹èªäœããã®ãã¹ãŠã®ç¥å ã®ãªã¹ããããã³ãããã®éã®æ¥ç¶ãæã«ããŠããŸãã ãã®ããŒã¿ãããã¡ãœãããå·Šããå³ã«æ€çŽ¢ãããã¯ã©ã¹ã®é åºä»ããªã¹ããäœæããå¿ èŠããããŸãã ãã®ãããªãªã¹ãã¯ãã¯ã©ã¹ã®ç·åœ¢åãšåŒã°ããŸãã ç°¡åã«ããããã«ã次ã®æ§é ãåããŸãã
察象 | A | B
ã¯ã©ã¹Bã®ç·åœ¢åã¯ãªã¹ã[BãAãobject]ã«ãªããŸãã ã€ãŸã BïŒïŒ.somethingïŒïŒãåŒã³åºããããšãã¡ãœããã¯æåã«ã¯ã©ã¹Bã§æ€çŽ¢ãããŸããããã§èŠã€ãããªãå Žåãã¯ã©ã¹Aã§æ€çŽ¢ãç¶è¡ãããŸããèŠã€ãããªãå Žåãæ€çŽ¢ã¯ã¯ã©ã¹ãªããžã§ã¯ãã§çµäºããŸãã ç·åœ¢åãããã¹ãŠã®ã¯ã©ã¹ãééããæ£ããã¡ãœãããèŠã€ãããªãã£ãåŸãPythonã¯å±æ§ãšã©ãŒãã¹ããŒããŸãã
è±åœ¢æ§é ã®åé¡ã解決ããã«ã¯ãç·åœ¢åã¯å調ã§ãªããã°ãªããŸããã ããã¯ãç¹å®ã®ã¯ã©ã¹Cã®ç·åœ¢åã§ã¯ã©ã¹Aãã¯ã©ã¹Bã«ç¶ãå ŽåïŒ[Cã...ãBã...ãA]ã®åœ¢åŒãæã£ãŠããïŒããã®åå«Dã¯ã©ã¹Bãç·åœ¢åã§Aã«ç¶ãããšãæå³ããŸãïŒ [Dã...ãCã...ãBã...ãA]ïŒã®ããã«ãªããŸãïŒã ã芧ã®ãšãããã¡ãœãã解決ã®å€ãé åºã¯å調ã§ã¯ãããŸããã ã¯ã©ã¹Aã®rhomboidæ§é ã®å Žåãç·åœ¢åã¯[Aããªããžã§ã¯ã]ãã¯ã©ã¹B-[Bããªããžã§ã¯ã]ã§ãããã¯ã©ã¹C-[CãAããªããžã§ã¯ããB]ã®å Žåãã¯ã©ã¹Bã«é¢ããŠå調æ§ããããã£ã«éåããŸãã
å調æ§ã®ç¹æ§ãæºããã«ã¯ããã®å Žåã2ã€ã®ç·åœ¢åãé©åã§ãïŒ[CãAãBãobject]ããã³[CãBãAãobject]ã æããã«ãäž¡æ¹ãšãã¯ã©ã¹AïŒäž¡æ¹ã®å Žåã«ãªããžã§ã¯ããAã«ç¶ãããïŒãŸãã¯ã¯ã©ã¹BïŒäž¡æ¹ã®å Žåã«ãªããžã§ã¯ããBã«ç¶ãããïŒã«é¢ããŠå調æ§ã«éåããŸããã ã©ã¡ããéžæããã®ã§ããïŒ ãã®å ŽåãæãçŽæçãªæ¹æ³ã¯ãã¯ã©ã¹Cã®å®çŸ©ãèŠãããšã§ããã¯ã©ã¹ãCïŒAãBïŒãšããŠå®£èšãããŠããå ŽåãBãAã«ç¶ããããæåã®ç·åœ¢åãè¡ãã®ãè³¢æã§ããã¯ã©ã¹ãCïŒBãA ïŒããã®åŸãAãBã«ç¶ã2çªç®ã®ç·åœ¢åãè¡ãæ¹ãè¯ãã§ãããã
ãã®éžæã¯ãããŒã«ã«ã®åªå é äœã«ãã£ãŠæ±ºå®ãããŸãã ããŒã«ã«åªå é äœã®ããããã£ã¯ã宣èšæãšåãé åºã®åå«ã¯ã©ã¹ã®ç·åœ¢åã«ããã芪ã¯ã©ã¹ã®éµå®ãå¿ èŠãšããŸãã ããšãã°ãã¯ã©ã¹ãDïŒAãBãCïŒãšããŠå®£èšãããŠããå ŽåãDã®ç·åœ¢åã§ã¯ãã¯ã©ã¹Aã¯Bã®åã«ãã¯ã©ã¹Bã¯Cã®åã«ãªããã°ãªããŸããã
äžéçµæãèŠçŽããã«ã¯ïŒ
- ã¯ã©ã¹ã®ç·åœ¢åã¯ãã¯ã©ã¹èªäœãšããã®ã¡ãœããã®å·Šããå³ãžã®é åºã§æ€çŽ¢ããããã¹ãŠã®ç¥å ïŒèŠªãšç¥å ïŒã®ãªã¹ãã§ãã
- ã¡ãœãã解決é åºïŒMROïŒã¯ãã¯ã©ã¹ã®ç·åœ¢åãã³ã³ãã€ã«ããæ¹æ³ã§ãã
- å調æ§ã¯ã芪ã¯ã©ã¹ã®ç·åœ¢åãšåãé åºã®èŠªã¯ã©ã¹ã®åå«ã¯ã©ã¹ã®ç·åœ¢åãé å®ããå¿ èŠãããããããã£ã§ãã
- ããŒã«ã«ã®åªå é äœã¯ãåã¯ã©ã¹ã®ç·åœ¢åã宣èšãšåã芪ã¯ã©ã¹ã®é åºã«åŸãããã«èŠæ±ããããããã£ã§ãã
C3ã¡ãœããã®è§£æ±ºã®ããã®è§£æ±ºã¢ã«ãŽãªãºã ã
äžèšã®ç®æšãéæããããã«ãPythonã¯C3ã¢ã«ãŽãªãºã ã䜿çšããŸãã ããã¯éåžžã«åçŽã§ãããç解ãæ·±ããããã«ã次ã®èŠåãå°å ¥ããŸãã
[C1ãC2ã... CN]-èŠçŽ C1ãC2ã... CNã®ãªã¹ãã ãããã£ãŠã[C]ã¯1ã€ã®èŠçŽ Cã®ãªã¹ãã§ãã
L [C]ã¯ã¯ã©ã¹Cã®ç·åœ¢åã§ããç·åœ¢åã¯å®çŸ©ã«ãããªã¹ãã§ããããšãèŠããŠããããšãéèŠã§ãã
mergeïŒL [C1]ãL [C2]ã...ãL [CN]ïŒ-ç·åœ¢åèŠçŽ L [C1]ãL [C2]ã...ãL [CN]ãäœããã®ã¢ã«ãŽãªãºã ã䜿çšããŠãªã¹ãã«ããŒãžããŸãã å®éããã®ã¢ã«ãŽãªãºã ã¯L [C1]ãL [C2]ã...ãL [CN]ãããã¹ãŠã®ã¯ã©ã¹ãé åºä»ããæçµãªã¹ãã®ã¯ã©ã¹ã®éè€ãé€å€ããå¿ èŠããããŸãã
ã¢ã«ãŽãªãºã C3ã¯ã次ã®ã«ãŒã«ã®ã»ããã§ãã
- ã¯ã©ã¹Cç·åœ¢åã¯ãã¯ã©ã¹Cèªäœããã®ã·ã³ã°ã«ãã³ãªã¹ãã«ããã®èŠªã®ç·åœ¢åãšãã®ãã¹ãŠã®èŠªã®ãªã¹ãã®åéåãå ãããã®ã§ãã å¡äŸã§ã¯ãã¯ã©ã¹Cã次ã®å ŽåãL [C] = [C] + mergeïŒL [C1]ãL [C2]ã...ãL [CN]ã[C1ãC2ã...ãCN]ïŒãšæžãããšãã§ããŸããã¯ã©ã¹CïŒC1ãC2ã...ãCNïŒãšããŠå®£èšãããŠããŸãã L [CX]ã®åç·åœ¢åã¯ã¯ã©ã¹CXã§å§ãŸããã¯ã©ã¹Cã®çŽæ¥ã®èŠªãšããŠãŠããªã³ãªã¹ãã®æåŸã«è¿œå ã§å²ãåœãŠãããŠããããšã«æ³šæããŠãã ããã
- ç·åœ¢åã®çµã¿åããã¯æ¬¡ã®ãšããã§ãã
- æåã®ç·åœ¢åãããŒãèŠçŽ ãååŸãããŸãïŒL [C1] [0]ïŒã
- ãã®èŠçŽ ã¯ãä»ã®ãã¹ãŠã®ç·åœ¢åïŒL [C2]ããL [CN]ãŸã§ïŒã§æ±ããããŸãã
- ãã®èŠçŽ ããªã¹ãã®å é 以å€ã®å Žæã§èŠã€ãã£ãå ŽåïŒL [CK] [X] == L [C1] [0]ãXïŒ= 0;æ¬è³ªçã«ãããã¯L [C1] [0]ãä»ã®èª°ãã®å ç¥ïŒãã¢ã«ãŽãªãºã ã¯æåã®ã¹ãããã«é²ã¿ã次ã®ç·åœ¢åïŒL [C2] [0]ïŒãããŒãèŠçŽ ãšããŠååŸããŸãã
- èŠçŽ ããŒã以å€ã®äœçœ®ã§èŠã€ãããªãå Žåãç·åœ¢åèŠçŽãªã¹ãã®æåŸã«è¿œå ãããåé¡ã®ãã¹ãŠã®ãªã¹ãããåé€ãããŸãïŒL [C1]ããL [CN]ãæçµã¯ã©ã¹ã§åãã¯ã©ã¹ã2åèŠã€ããããšã¯ã§ããŸããïŒ ã èŠçŽ ãåé€ããåŸã«ç©ºã®ãªã¹ããããå Žåããããã¯çµåããé€å€ãããŸãã ãã®åŸãã¢ã«ãŽãªãºã ã¯æåããïŒæ°ããèŠçŽ L [C1] [0]ããïŒç¹°ãè¿ãããŸãïŒååšããå ŽåïŒã ååšããªãå Žåãçµåã¯å®äºããŸãã
- ã¢ã«ãŽãªãºã ãL [CN]ã®æåŸã®èŠçŽ ã«å°éãããããŒãèŠçŽ ãã«ãŒã«ãæºãããªãå Žåãç·åœ¢åã¯äžå¯èœã§ãã
L [C] = [C] +ããŒãžïŒL [A]ãL [B]ã[AãB]ïŒ
L [A] = [A] +ããŒãžïŒL [ãªããžã§ã¯ã]ã[ãªããžã§ã¯ã]ïŒ
L [B] = [B] +ããŒãžïŒL [ãªããžã§ã¯ã]ã[ãªããžã§ã¯ã]ïŒ
L [ãªããžã§ã¯ã] = [ãªããžã§ã¯ã]ïŒçž®éã±ãŒã¹ïŒ
çµ±åã®ããã»ã¹ã¯æãèå³æ·±ãã®ã§ããã詳现ã«åæããŸãã L [A]ããã³L [B]ã®å ŽåãåŒmergeïŒL [object]ã[object]ïŒ= mergeïŒ[object]ã[object]ïŒã¯ç°¡åã«å±éããŸãã 1çªç®ãš2çªç®ã®ãªã¹ãã¯äž¡æ¹ãšã1ã€ã®ãªããžã§ã¯ãèŠçŽ ã§æ§æãããŠããããããŠããªã³ã«ãŒã«ã®æ¡é 4ã«åŸã£ãŠãçµæã¯[ãªããžã§ã¯ã]ã«ãªããŸãã
L [C] = [C] + mergeïŒL [A]ãL [B]ã[AãB]ïŒ= [C] + mergeïŒ[Aãobject]ã[Bãobject]ã[A ãB]ïŒã C3ã®ã«ãŒã«ã«åŸã£ãŠçµåãæžããŸãã
- æåã®ãªã¹ãã®ãŒãèŠçŽ ãååŸããŸã-A
- ä»ã®ãã¹ãŠã®ãªã¹ãã§ãããæ¢ããŸãããã [Bãobject]ããã³[AãB]ã§ã
- ã¯ã©ã¹Aã¯ãªã¹ã[Bãobject]ã«èŠã€ãããããªã¹ã[AãB]ã¯ãŒãèŠçŽ ã§ãããããæçµç·åœ¢åãªã¹ãL = [] + [A] = [A]ã«è¿œå ããŸãã ãã®åŸããŠããªã³ã®ãã¹ãŠã®ãªã¹ãããã¯ã©ã¹Aãåé€ããå¿ èŠããããŸãã L [C] = [C] + [A] + mergeïŒ[ãªããžã§ã¯ã]ã[Bããªããžã§ã¯ã]ã[B]ïŒãååŸããŸãã
- ç¹°ãè¿ããŸãããæåã®ãªã¹ãã®ãŒãèŠçŽ -ãªããžã§ã¯ããååŸããŸãã
- ä»ã®ãã¹ãŠã®ãªã¹ãã調ã¹ãŠããªããžã§ã¯ãããªã¹ãã®æåã®ïŒnullã§ã¯ãªãïŒèŠçŽ ã§ããããšãèŠã€ããŸããã[Bãobject]ã äžèšã®ããã«ãæ¬è³ªçã«ãããã¯ã¯ã©ã¹ãªããžã§ã¯ããã¯ã©ã¹Bã®ç¥å ã§ããããšãæå³ããŸãããããã£ãŠãæåã¯ã¯ã©ã¹Bã§ããå ·äœçãªã¡ãœãããæ¢ãããšãçã«ããªã£ãŠããŸãããããã£ãŠãã¢ã«ãŽãªãºã ã¯ã¹ããã1ã«ããªãæ»ããŸãã
- åã³ããŒãèŠçŽ ãåããŸãããä»åã¯2çªç®ã®ãªã¹ããã€ãŸã ãªã¹ã[Bããªããžã§ã¯ã]ããã®Bã
- ä»ã®ãªã¹ãã§ãããæ¢ãããŒãã®äœçœ®ã«ãã3çªç®ã®ãªã¹ã[B]ã§ã®ã¿èŠã€ããŸãã ãããã£ãŠãæçµãªã¹ãã«è¿œå ããŠããããŠããªã³å ã®ãã¹ãŠã®ãªã¹ãããåé€ããŸãã L = [A] + [B] = [AãB]ã«ãªããããã«å¿ããŠL [C] = [C] + [AãB] + mergeïŒ[object]ã[object]ïŒãåŸãããŸãã
- çµæãšããŠçããããŒãžãŠããªã³ïŒ[ãªããžã§ã¯ã]ã[ãªããžã§ã¯ã]ïŒã¯ãã§ã«äžã§è°è«ãããŠããŸãã çµæãšããŠãL [C] = [C] + [AãB] + [object] = [C] + [AãBãobject] = [CãAãBãobject]ãåŸãããŸãã ãªã¹ã[AãBããªããžã§ã¯ã]ã¯ãããŒãžã¢ãœã·ãšãŒã·ã§ã³ã®çµæã§ãïŒL [A]ãL [B]ã[AãB]ïŒã
察象 | D | \ | E | / C
ç·åœ¢åL [C]ãæžããŸãã
L [C] = [C] +ããŒãžïŒL [D]ãL [E]ã[DãE]ïŒ
L [E] = [E] +ããŒãžïŒL [D]ã[D]ïŒ
L [D] = [D] +ããŒãžïŒL [ãªããžã§ã¯ã]ã[ãªããžã§ã¯ã]ïŒ= [Dããªããžã§ã¯ã]
眮æãå®è¡ããŠååŸããŸãã
L [E] = [E] +ããŒãžïŒ[Dããªããžã§ã¯ã]ã[D]ïŒ= [EãDããªããžã§ã¯ã]
L [C] = [C] +ããŒãžïŒ[Dããªããžã§ã¯ã]ã[EãDããªããžã§ã¯ã]ã[DãE]ïŒ
ä»ãç§ãã¡ãåŸããã®ãèŠãŠãã ããã ãªã¹ã[Dãobject]ããã³[DãE]ã§ã¯ãŒãèŠçŽ ãDã§ãããããããŒãžããŒãžïŒ[Dãobject]ã[EãDãobject]ã[DãE]ïŒã¯å®äºã§ããŸããããªã¹ãé ç®[EãDããªããžã§ã¯ã]ã éã«ã[EãDãobject]ã®ãã«èŠçŽ ã§ããEã¯ã[DãE]ã®æåã®èŠçŽ ã§ããããŸãã ãããã£ãŠã3åã®å埩ã®åŸãã¢ã«ãŽãªãºã ã¯ã¹ããã5ã«é²ã¿ããã®åŸPythonã¯TypeErrorãšã©ãŒãã¹ããŒããŸãïŒããŒã¹DãEã«å¯ŸããŠäžè²«ããã¡ãœãã解決é åºïŒMROïŒãäœæã§ããŸãããçµåã芪ã®ãªã¹ãã§çµäºããªãã£ãå Žåãé åºéåãçºçããŸããããŒã«ã«ã®å¹ŽååºåïŒL [C] = [C] +ããŒãžïŒ[Dããªããžã§ã¯ã]ã[EãDããªããžã§ã¯ã]ïŒ= [C] + [E] +ããŒãžïŒ[Dããªããžã§ã¯ã]ã[Dããªããžã§ã¯ã] ïŒ= [C] + [EãD] + [object] = [CãEãDãobject]ã ã¯ã©ã¹Cã®ãã®ãããªç·åœ¢åã§ã¯ãæ€çŽ¢ã¯æåã«ã¯ã©ã¹Eã§å®è¡ããã次ã«ã¯ã©ã¹Dã§å®è¡ãããŸãããCïŒDãEïŒã¯å®£èšã§èšè¿°ãããŠããŸãã
ãã®åé¡ã解決ããããšã¯é£ãããããŸããã ã¯ã©ã¹CïŒEãDïŒã®å®£èšãæžãçŽãã ãã§ååã§ãã ãã®å Žåã次ã®ããã«ãªããŸãã
L [C] = [C] +ããŒãžïŒ[EãDããªããžã§ã¯ã]ã[Dããªããžã§ã¯ã]ã[EãD]ïŒ= [C] + [E] +ããŒãžïŒ[Dããªããžã§ã¯ã]ã[D ããªããžã§ã¯ã]ã[D]ïŒ= [C] + [EãDããªããžã§ã¯ã] = [CãEãDããªããžã§ã¯ã]ã
å®éãã¯ã©ã¹å®£èšã§ã¯ã芪ããªã¹ãããé åºã¯ã¯ã©ã¹ã®ç·åœ¢åãšåãã§ãããã€ãŸãã å°å ã®å¹Žååºåãå°éãããŸãã Pythonã¯èŠªã瀺ãã®ãããè«ççãªé åºã§ããããšã瀺åããŠããŸãããã¡ã¿ã¯ã©ã¹ãä»ããŠç¬èªã®MROã宣èšããå Žåãåéºãæ¢ãããšãæ¢ããããšã¯ãããŸããã ããããããã«ã€ããŠã¯ããããçµããã§ãã
Pythonã¯ãã¯ã©ã¹ã®äœææã«ç·åœ¢åã1åèšç®ããŸãã èªå·±ãã¹ãããã®ä»ã®ç®çã§ååŸããå Žåã¯ã__ mro__ã¯ã©ã¹ããããã£ïŒããšãã°ãC .__ mro__ïŒã䜿çšããŸãã çŽ æãçµ±åããããã«ã貧ãã人ã ã®äŸãèŠãŠã¿ãŸãããã ãªããžã§ã¯ãã¯ã©ã¹ã¯ãç·åœ¢åãæ··ä¹±ãããªãããã«æå³çã«ã¹ããŒãããŸãã äžèšãããããããã«ãåäžç¶æ¿ã§ã¯ãã¯ã©ã¹ã¯åå«ããç¥å ã«åçŽã«äžŠãã§ããããããªããžã§ã¯ãã¯åžžã«ãã§ãŒã³ã®æåŸã«ãªããŸãã ãã®ä»ã ç§ã¯æçã®å°é家ã§ãé³æ¥œå®¶ã§ããªãã®ã§ãäŸã¯åãªãäŸã§ãã ãããã®æå³ã®äžæ£ç¢ºãã«çŠç¹ãåãããã¹ãã§ã¯ãããŸããã
ãã¥ãŒãžã㯠/ \ ããã¯ãŽã·ãã¯------ / \ / \ ã¡ã¿ã«ãŽã·ãã¯ããã¯\ | | \ \ ------------------ãŽã·ãã¯ã¡ã¿ã« | / 69ç®
class Music (object): pass
class Rock (Music): pass
class Gothic (Music): pass
class Metal (Rock): pass
class GothicRock (Rock, Gothic): pass
class GothicMetal (Metal, Gothic): pass
class The69Eyes (GothicRock, GothicMetal): pass
L [The69Eyes] = [The69Eyes] + mergeïŒL [GothicRock]ãL [GothicMetal]ã[GothicRockãGothicMetal]ïŒ
L [GothicRock] = [GothicRock] + mergeïŒL [ããã¯]ãL [ãŽã·ãã¯]ã[ããã¯ããŽã·ãã¯]ïŒ
L [GothicMetal] = [GothicMetal] + mergeïŒL [éå±]ãL [ãŽã·ãã¯]ã[éå±ããŽã·ãã¯]ïŒ
L [ããã¯] = [ããã¯ãé³æ¥œ]
L [ãŽã·ãã¯] = [ãŽã·ãã¯ãé³æ¥œ]
L [éå±] = [éå±] + [ããã¯ãé³æ¥œ] = [éå±ãããã¯ãé³æ¥œ]
眮æåŸïŒ
L [GothicRock] = [GothicRock] + mergeïŒ[ããã¯ãé³æ¥œ]ã[ãŽã·ãã¯ãé³æ¥œ]ã[ããã¯ããŽã·ãã¯]ïŒ= [GothicRockãããã¯ããŽã·ãã¯ãé³æ¥œ]
L [GothicMetal] = [GothicMetal] + mergeïŒ[éå±ãããã¯ãé³æ¥œ]ã[ãŽã·ãã¯ãé³æ¥œ]ã[éå±ããŽã·ãã¯]ïŒ= [GothicMetal] + [éå±ãããã¯ããŽã·ãã¯ãé³æ¥œ] = [GothicMetalãéå±ãããã¯ããŽã·ãã¯ãé³æ¥œ]
L [The69Eyes] = [The69Eyes] + mergeïŒ[GothicRockãRockãGothicãMusic]ã[GothicMetalãMetalãRockãGothicãMusic]ã[GothicRockãGothicMetal]ïŒ
= [The69Eyes] + [GothicRockãGothicMetal] + mergeïŒ[ããã¯ããŽã·ãã¯ãé³æ¥œ]ã[éå±ãããã¯ããŽã·ãã¯ãé³æ¥œ]ïŒ
= [The69Eyes] + [GothicRockãGothicMetalãMetal] + mergeïŒ[ããã¯ããŽã·ãã¯ãé³æ¥œ]ã[ããã¯ããŽã·ãã¯ãé³æ¥œ]ïŒ
= [The69EyesãGothicRockãGothicMetalãã¡ã¿ã«ãããã¯ããŽã·ãã¯ãé³æ¥œ]
é£ã¹ç©------- / \ \ èç² | \ \ / ãŠãµã®è±èããŒã¹ã \ | | / ãã€
class Food (object): pass
class Meat (Food): pass
class Milk (Food): pass
class Flour (Food): pass
class Rabbit (Meat): pass
class Pork (Meat): pass
class Pasty (Milk, Flour): pass
class Pie (Rabbit, Pork, Pasty): pass
L [ãã€] = [ãã€] +ããŒãžïŒL [ãŠãµã®]ãL [ããŒã¯]ãL [ããŒã¹ããªãŒ]ã[ãŠãµã®ãããŒã¯ãããŒã¹ãç¶]ïŒ
L [ãŠãµã®] = [ãŠãµã®] +ããŒãžïŒL [è]ã[è]ïŒ
L [è±è] = [è±è] +ããŒãžïŒL [è]ã[è]ïŒ
L [Pasty] = [Pasty] + mergeïŒL [çä¹³]ãL [å°éºŠç²]ã[çä¹³ãå°éºŠç²]ïŒ
L [è] = [è] +ããŒãžïŒL [é£å]ã[é£å]ïŒ= [èãé£å]
L [çä¹³] = [çä¹³] +ããŒãžïŒL [é£ç©]ã[é£ç©]ïŒ= [çä¹³ãé£ç©]
L [å°éºŠç²] = [å°éºŠç²] +ããŒãžïŒL [é£å]ã[é£å]ïŒ= [å°éºŠç²ãé£å]
眮æåŸã次ã®ããã«ãªããŸãã
L [ãŠãµã®] = [ãŠãµã®ãèãé£ç©]
L [è±è] = [è±èãèãé£ã¹ç©]
L [Pasty] = [Pasty] + mergeïŒ[çä¹³ãé£å]ã[å°éºŠç²ãé£å]ã[çä¹³ãå°éºŠç²]ïŒ= [Pasty] + [çä¹³ãå°éºŠç²ãé£å] = [Pastyãçä¹³ãå°éºŠç²ãé£å]
L [ãã€] = [ãã€] +ããŒãžïŒ[ãŠãµã®ãèãé£å]ã[è±èãèãé£å]ã[éŠå³ããã«ã¯ãå°éºŠç²ãé£å]ã[ãŠãµã®ãè±èãããŒã¹ãç¶]ïŒ
= [ãã€] + [ãŠãµã®] +ããŒãžïŒ[èãé£å]ã[è±èãèãé£å]ã[ããŒã¹ããçä¹³ãå°éºŠç²ãé£å]ã[è±èãããŒã¹ãç¶]ïŒ
= [ãã€] + [ãŠãµã®ãè±è] +ããŒãžïŒ[èãé£å]ã[èãé£å]ã[ããŒã¹ãããã«ã¯ãå°éºŠç²ãé£å]ã[ããŒã¹ã]ïŒ
= [ãã€] + [ãŠãµã®ãããŒã¯ãè] +ããŒãžïŒ[ããŒã]ã[ããŒã]ã[ãã¹ãã£ããã«ã¯ãå°éºŠç²ãããŒã]ã[ãã¹ãã£]ïŒ
= [ãã€] + [ãŠãµã®ãè±èãèããã¹ãã£] +ããŒãžïŒ[é£å]ã[é£å]ã[çä¹³ãå°éºŠç²ãé£å]ïŒ
= [ãã€] + [ãŠãµã®ãè±èãèãããŒã¹ãããã«ã¯ãå°éºŠç²ãé£å]
= [ãã€ããŠãµã®ãè±èãèãããŒã¹ãããã«ã¯ãå°éºŠç²ãé£å]
å ç¥ãžã®é£çµ¡æ¹æ³ã
å€éç¶æ¿ã«ã¯å¥ã®ç¹å®ã®åé¡ããããŸãã å®éã芪ã¯ã©ã¹ã®ã¡ãœããã®çŽæ¥æ€çŽ¢ã¯ã芪ã¯ã©ã¹ãã掟çã§ããå©ç¹ã®äžéšã«ãããŸããã åäžç¶æ¿ã®å Žåã®ããã«ãããã€ãã®ã¢ã¯ã·ã§ã³ã«å ããŠãåã芪ã¡ãœãããåŒã³åºãã¡ãœãããåå«ã«å®è£ ããããšã«ãããå€ãã®å Žåãç掻ã楜ã«ããããšãã§ããŸãã ããšãã°ãããªãé »ç¹ã«ãããèŠã€ããããšãã§ããŸãïŒ
class B (A):
def __init__ (self):
# something
A. __init__ (self)
ãã ããå€éç¶æ¿ã®å Žåããã®ã¢ãããŒãã¯é©ããŠããŸããã ãããŠãã©ã®ãããªçç±ã§ïŒ
class C (B, A):
def __init__ (self):
# something
B. __init__ (self)
A. __init__ (self)
æåã«ã芪ã¯ã©ã¹ãæ瀺çã«åç §ããŸãïŒå®éãåäžç¶æ¿ã®å Žåãåæ§ã§ãïŒã ç¥å ã®1ã€ãå¥ã®ã¯ã©ã¹ã«çœ®ãæããããå®å šã«åé€ããå Žåã¯ãã¢ã¯ã»ã¹ãããã¹ãŠã®é¢æ°ãå€æŽããå¿ èŠããããŸãã äœããèŠéããšãããã«ã¯ãã°ããããããããŸãã ããããããã¯ãŸã åé¡ã®ååã§ãã 第äºã«ãã¯ã©ã¹Aãšã¯ã©ã¹Bã«ã€ããŠã¯äœãç¥ããŸãããããããããããã¯åããããªæ¹æ³ã§åç §ããå ±éã®ç¥å ãæã£ãŠããŸãã
class A (P1, P2):
def __init__ (self):
# something
P1. __init__ (self)
P2. __init__ (self)
class B (P1, P2):
def __init__ (self):
# something
P1. __init__ (self)
P2. __init__ (self)
ãã®å Žåãå ±éã®ç¥å ã®åæåã2åæ©èœããããšãããããŸãã ããã¯æ£ãããããŸããã ãããé¿ããããã«ãPythonã«ã¯ã¹ãŒããŒã¯ã©ã¹ããããŸãã ããŒãžã§ã³3.0ã§ã¯ã圌ã¯æçµçã«äººéåããã次ã®ããã«ã¢ã¯ã»ã¹ã§ããŸãã
class C (B, A):
def __init__ (self):
# something
super (). __init__ () # 3.0 super(C, self)
ããŒãžã§ã³2.xã§ã¯ãæåã®åŒæ°ã¯èŠªã§ã¯ãªãã¯ã©ã¹èªäœãæå®ããå¿ èŠãããããšã«æ³šæããŠãã ããã å®éãã¹ãŒããŒã¯ã©ã¹ãªããžã§ã¯ãã¯ãåæåæã«æž¡ãããåŒæ°ãèšæ¶ããã¡ãœããïŒäžèšã®äŸã®superïŒïŒ.__ init __ïŒselfïŒïŒãåŒã³åºããšã2çªç®ã®åŒæ°ã¯ã©ã¹ïŒself .__ class __.__ mro__ïŒã®ç·åœ¢åãªã¹ãã調ã¹ãŠããã®ã¡ãœãããåŒã³åºãããšããŸãåæ§ã«ãæåã®åŒæ°ïŒã¯ã©ã¹CïŒã®ã¯ã©ã¹ã«ç¶ããã¹ãŠã®ã¯ã©ã¹ã«å¯ŸããŠãæåã®åŒæ°ïŒselfïŒããã©ã¡ãŒã¿ãŒãšããŠæž¡ããŸãã ã€ãŸã ç§ãã¡ã®å ŽåïŒ
self .__ class __.__ mro__ = [CãBãAãP1ãP2ã...]
superïŒCãselfïŒ.__ init __ïŒïŒ=> B .__ init __ïŒselfïŒ
superïŒBãselfïŒ.__ init __ïŒïŒ=> A .__ init __ïŒselfïŒ
superïŒAãselfïŒ.__ init __ïŒïŒ=> P1 .__ init __ïŒselfïŒ
ã芧ã®ãšãããB .__ init__ã¡ãœãããããsuperã䜿çšãããšãA .__ init__ã¡ãœãããåŒã³åºãããŸãããã¯ã©ã¹Aã¯ãããšã¯é¢ä¿ããªããå ç¥ã§ã¯ãããŸããã ãã®å Žåãå¿ èŠã«å¿ããŠããã§ãŒã³ã¯ãã¹ãŠã®ç¥å ã®ã¡ãœãããäžåºŠã«åŠçããŸãã
ãã®ã¢ãããŒãã®ãããã§ãããšãã°ã調æ»å¯Ÿè±¡ã®ãã€ã®äŸã®ãã¹ãŠã®ã³ã³ããŒãã³ãã«ã¢ã¬ã«ã²ã³ã¡ãœãããå°å ¥ã§ããŸããããã¯ãç¥å ã®ãã§ãŒã³ãäžè²«ããŠééããŠããã€ã€ãŒã«èŠåããããã«ãã¹ãŠã®ã¢ã¬ã«ã²ã³ã³ã³ããŒãã³ãã®ãªã¹ããäœæããŸãã ã³ã³ããŒãã³ãã³ã³ããŒãã³ãããç¶æ¿ããããã ãã«ãå補åã®ãæšå¥šãããªãããªã¹ããæžãæããããã䟿å©ã§ãã ãã®åŸããªã¹ããèªåçã«çæãããŸãã åæ§ã«ãç¹å®ã®ã°ã«ãŒãããç¹å®ã®æžè¡°ä¿æ°ãæã€ãžã£ã³ã«ãŸã§ãå補åã®é£²ã¿ç©ã®éžæããŠãŒã¶ãŒã«æäŸããããèªäœã®ã€ã³ã¿ãŒãããã©ãžãªã¹ããŒã·ã§ã³ã§ãŠãŒã¶ãŒã®å¥œã¿ã®éã¿ãå€ãããããããšãã§ããŸãã
åã°ã©ãã®äŸã¯æ¬¡ã®ããã«ãªããŸãïŒããŒãžã§ã³2.xã®å ŽåïŒïŒ
class Food (object):
def drink(self):
return [ 'Water' , 'Cola' ]
def allergen(self):
return []
class Meat (Food):
def drink(self):
return [ 'Red wine' ] + super (Meat, self).drink()
class Milk (Food):
def allergen(self):
return [ 'Milk-protein' ] + super (Milk, self).allergen()
class Flour (Food): pass
class Rabbit (Meat):
def drink(self):
return [ 'Novello wine' ] + super (Rabbit, self).drink()
class Pork (Meat):
def drink(self):
return [ 'Sovinion wine' ] + super (Pork, self).drink()
def allergen(self):
return [ 'Pork-protein' ] + super (Pork, self).allergen()
class Pasty (Milk, Flour): pass
class Pie (Rabbit, Pork, Pasty):
def drink(self):
return [ 'Mineral water' ] + super (Pie, self).drink()
if __name__ == "__main__" :
pie = Pie()
print 'List of allergens: '
for allergen in pie.allergen(): print ' - ' + allergen
print 'List of recommended drinks: '
for drink in pie.drink(): print ' - ' + drink
ãã®çµæã次ã®ããã«è¡šç€ºãããŸãã
ã¢ã¬ã«ã²ã³ã®ãªã¹ãïŒ
-è±èã¿ã³ãã¯è³ª
-ãã«ã¯ãããã€ã³
æšå¥šããã飲ã¿ç©ã®ãªã¹ãïŒ
-ããã©ã«ãŠã©ãŒã¿ãŒ
-ããŽã§ãã¯ã€ã³
-ãœãããªã³ã¯ã€ã³
-èµ€ã¯ã€ã³
-æ°Ž
-ã³ãŒã©
ãã®ãªã¹ãã«åºã¥ããŠãç·åœ¢åãªã¹ããã©ã®ããã«åŠçãããããç解ã§ããŸãã ã芧ã®ãšããã芪ã¡ãœããã¯2ååŒã³åºãããŠããŸãããããã§ãªããšãã¢ã¬ã«ã²ã³ãŸãã¯æšå¥šã®ãªã¹ãã«ç¹°ãè¿ããèŠã€ãããŸãã ããã«ãæãå€ãã¯ã©ã¹ã®Foodã§ã¯ãã¢ã¬ã«ã²ã³ãšé£²ã¿ç©ã®äž¡æ¹ã®ã¡ãœãããå®çŸ©ãããŠããŸãã superïŒïŒåŒã³åºãã¯ãã§ãã¯ãå®è¡ããªããããååšããªãã¡ãœãããåŒã³åºãããšãããšãAttributeErrorïŒ 'super' object has no attribute 'allergen'ã®ãããªãšã©ãŒãçºçããŸãã
ç·åœ¢åãã³ã³ãã€ã«ã§ããªãå Žåã
C3ã¢ã«ãŽãªãºã ã䜿çšããŠç·åœ¢åãæ§æããããšãäžå¯èœãªå Žåã«ã€ããŠã¯ããã§ã«æ€èšããŸããã ãã ãããã®åŸãåå«ã¯ã©ã¹ã®å®£èšã§å ç¥ã¯ã©ã¹ã®å Žæãå€æŽããããšã§åé¡ã¯è§£æ±ºããŸããã ç·åœ¢åãäžå¯èœãªä»ã®ã±ãŒã¹ããããŸãã
class C (A, B): pass
class D (B, A): pass
class E (C, D): pass
L [E] = [E] +ããŒãžïŒL [C]ãL [D]ã[CãD]ïŒ= [E] +ããŒãžïŒ[CãAãB]ã[DãBãA]ã [CãD]ïŒ
= [E] + [CãD] +ããŒãžïŒ[AãB]ã[BãA]ïŒ
ã芧ã®ãšããã宣èšCã§ã¯ã¯ã©ã¹AãBã®åã«ããã宣èšDã§ã¯éã«ãªã£ãŠããããã競åã¯è§£æ±ºãããŸããã§ããã ããŠããã®å Žæããæ§é ã確èªããå¿ èŠããããŸãã ããããæ¬åœã«éãéãããå¿ èŠãããå ŽåããŸãã¯èªåãäœãããŠããã®ããç¥ã£ãŠããã ãã§ããã°ãPythonã¯ããªããå¶éããŸããã ã¡ã¿ã¯ã©ã¹ãä»ããŠç¬èªã®ç·åœ¢åãå®çŸ©ã§ããŸãã ãããè¡ãã«ã¯ãã¡ã¿ã¯ã©ã¹ã§mroïŒclsïŒã¡ãœãããæå®ããã ãã§ååã§ããå®éãããŒã¹ã¡ã¿ã¯ã©ã¹ã¿ã€ãã®ã¡ãœãããåå®çŸ©ãããšãå¿ èŠãªç·åœ¢åãè¿ãããŸãã
class MetaMRO ( type ):
def mro(cls):
return (cls, A, B, C, D object)
ããã«ãããŒãžã§ã³3.0ãš2.xã§ã¯ã¯ã©ã¹å®£èšãç°ãªããŸãã
class E (C, D): __metaclass__ = MetaMRO # 2.x
class E (C, D, metaclass = MetaMRO): pass # 3.0
ãã®åŸãE .__ mro__ = [EãAãBãCãDãobject]ã MROãæ åœããå ŽåãPythonã¯è¿œå ã®ãã§ãã¯ãè¡ãããåå«ãããå ã«å ç¥ãå®å šã«æ€çŽ¢ã§ããããšã«æ³šæããŠãã ããã ããã¯æãŸããããšã§ã¯ãããŸããããå¯èœã§ãã
䟿å©ãªãªã³ã¯ïŒ
Python 2.2ã§ã®åãšã¯ã©ã¹ã®çµ±å -ãªããžã§ã¯ãã¢ãã«ãšã¡ã¿ããŒã¿ã¢ãã«ã®åé¢ã«ã€ããŠã ãŸããMROãå€éç¶æ¿ã®åé¡ãããã³superïŒïŒã«ã€ããŠã説æããŸãã
Python 2.3ã¡ãœãã解決é åº -äŸä»ãã®C3ã¢ã«ãŽãªãºã ã æåŸã«ãçŽç²ãªPythonã§ã®mroããã³mergeé¢æ°ã®å®è£ ããããããã¹ããããã³ãŒããããç解ããŠãã人åãã§ãã
ãã£ã©ã³ã®å調ã¹ãŒããŒã¯ã©ã¹ç·åœ¢å -ããã€ãã®ã¿ã€ãã®ç·åœ¢åã®æ¯èŒã
ããšããã
ãã¡ãããé¢é£ãããã¹ãŠã®ãããã¯ãã«ããŒããããšã¯äžå¯èœã§ããããããã誰ããçããããå€ãã®è³ªåãæã£ãŠããŸãã ããšãã°ãã¡ã¿ã¯ã©ã¹ãã¿ã€ããšãªããžã§ã¯ãã®åºæ¬ã¯ã©ã¹ãªã©ã§ãã èå³ãããå Žåã¯ãæéãçµã€ã«ã€ããŠãç§ã¯ãã®ãããªãããã¯ã解æããããšããããšãã§ããŸãïŒ
- Pythonã®ã€ã³ããã¹ãã¯ã·ã§ã³ïŒå¯Ÿè©±åãã«ããdirãsysãªã©ã
- ã¿ã€ããšãªããžã§ã¯ãïŒã»ãšãã©ã®å Žåã ããã®ç¡æã®ççž®ããã翻蚳ãšããŠ
- Pythonã®éæ³ïŒ__str __ã__ init __ã__ new __ã__ slots__ãªã© å®éãããªãããããããã®ã§ãããã€ãã®éšåãèŠã€ããããšãã§ããŸãã
- ã¡ã¿ã¯ã©ã¹ã
PSïŒãã®ãããã¯ãšå人çã«Goodroneãæçš¿ããŠããããã¹ãŠã®äººã«æè¬ããŸãã