ðpythonã®ãã³ã¬ãŒã¿ PEP318
def foo(self):
perform method operation
foo = classmethod(foo)
ããã¯é·ãã¡ãœããã§ã¯èªã¿ã«ãããªããŸãããŸããæŠå¿µçã«ã¯1ã€ã®å®£èšã§ããé¢æ°ã«3åãååãã€ããã®ã¯pythonicã§ã¯ãããŸããããã®åé¡ã®è§£æ±ºçã¯ãã¡ãœããã®å€æãã¡ãœããèªèº«ã®å®£èšã«è¿ã¥ããããšã§ããæ°ããæ§æã®æå³ã¯
def foo(cls):
pass
foo = synchronized(lock)(foo)
foo = classmethod(foo)
ããé¢æ°ã®å®£èšã«è£ 食ã眮ããšãã代æ¿æ¡ã§çœ®ãæããããšãã§ããïŒ
@classmethod
@synchronized(lock)
def foo(cls):
pass
ãã®æ¹æ³ã§ã¯ã©ã¹ãå€æŽããããšãå¯èœã ãããã®å©ç¹ã¯ããã«ã¯ããããªããã»ãŒééããªããã¯ã©ã¹ãã³ã¬ãŒã¿ãŒã§ã§ããããšã¯ãã¹ãŠã¡ã¿ã¯ã©ã¹ã§ãã§ããŸãããã¡ã¿ã¯ã©ã¹ã䜿ãããšã¯ååã«ãããã«ããã®ã§ãã¯ã©ã¹ãç°¡åã«ä¿®æ£ããæ¹æ³ãããããšã¯é åçã§ããPython 2.4 ã§ã¯ãé¢æ°/ã¡ãœãããã³ã¬ãŒã¿ã®ã¿ãè¿œå ãããŠããŸãã
ãªããããªã«é£ããã®ãïŒ
Pythonã§ã¯ããŒãžã§ã³2.2ãã2ã€ã®ãã³ã¬ãŒã¿ïŒclassmethod()ãšstaticmethod()ïŒã䜿ããããã«ãªããŸããããã®é ããããããäœããã®æ§æçãªãµããŒããPythonã«è¿œå ãããã ãããšæãããŠããŸããããã®åæãèãããšããªãã³ã³ã»ã³ãµã¹ãåŸãã®ãé£ããã®ãäžæè°ã«æããããããŸãããcomp.lang.python ãš python-dev ã¡ãŒãªã³ã°ãªã¹ãã®äž¡æ¹ã§ãé¢æ°ãã³ã¬ãŒã¿ãã©ã®ããã«å®è£
ããã®ããã¹ããªã®ããè°è«ã亀ããããŠããŸããããªãããããªããã°ãªããªãããšããæ確ãªçç±ã¯ãããŸããããããã€ãã®åé¡ãæãæèŠãåãããŠããããã§ãã
æå³ã®å®£èšããã©ãã«å±ãããã«ã€ããŠã®æèŠã®çžéã
é¢æ°ã®å®çŸ©ã®æåŸã§è£
食ãå€æãè¡ãã®ã¯æé©ã§ã¯ãªããšããç¹ã§ã¯ãã»ãŒå
šå¡ãåæããŠããããã以äžã«ããã®æ
å ±ãã©ãã«çœ®ãã¹ããã«ã€ããŠã®æ確ãªã³ã³ã»ã³ãµã¹ã¯ãªãããã ã
æ§æäžã®å¶çŽ Pythonã¯æ§æçã«åçŽãªèšèªã§ããã(èŠèŠçã«ãèšèªããŒãµã«é¢ããŠã) "ç©äºãå°ç¡ãã«ãã "ããšãªãäœãã§ããŠäœãã§ããªããã«ã€ããŠãããªã匷ãå¶çŽããããŸãããã®æŠå¿µãåããŠç¥ã£ã人ãã¡ãã"ãããããã ãäœããã£ãŠããã®ãããã£ã "ãšæãããã«ããã®æ
å ±ãæ§é åããæçœãªæ¹æ³ã¯ãªããå¯èœã ãšæãããæåã®æ¹æ³ã¯ãæ°ãããŠãŒã¶ãŒãæ§æãæå³ãããã®ã«ã€ããŠãèåç¡çšœãªééã£ãã¡ã³ã¿ã«ã»ã¢ãã«ãäœããªãããã«ããããšã§ããã
ã³ã³ã»ããã«å¯Ÿããå
šäœçãªäžæ
£ããã代æ°åŠïŒãããã¯åºæ¬çãªç®æ°ïŒãäžéãç¥ã£ãŠãã人ããä»ã®ããã°ã©ãã³ã°èšèªãå°ãªããšã1ã€äœ¿ã£ãããšããã人ã«ãšã£ãŠã¯ãPythonã®å€ãã¯çŽæçã§ããPythonã§ãã³ã¬ãŒã¿ã®æŠå¿µã«åºäŒãåã«ããã³ã¬ãŒã¿ã®æŠå¿µã«è§Šããããšããã人ã¯ã»ãšãã©ããªãã§ãããããã®æŠå¿µãæãã匷åãªæ¢åã®ããŒã ããªãã®ã§ãã
äžè¬çã«æ§æã®è°è«ã¯ãä»ã®äœãããå€ãã®è«äºãåŒãèµ·ããããã§ããPEP 308ã«é¢é£ããäžé
æŒç®åã®è°è«ããã®äžäŸã§ãã
èæ¯
çŸç¶ã§ã¯æ§æãµããŒããæãŸãããšããã®ãäžè¬çãªæèŠã§ããGuido ã¯ç¬¬10å Python ã«ã³ãã¡ã¬ã³ã¹ã® DevDay åºèª¿è¬æŒã§ãã³ã¬ãŒã¿ã®æ§æãµããŒãã«ã€ããŠèšåããŸãããMichael Hudsonã¯ã«ã³ãã¡ã¬ã³ã¹ã®çŽåŸã«python-devã§ãã®ãããã¯ãåãäžããæåã®æ¬åŒ§ä»ãæ§æã¯Gareth McCaughanã«ããcomp.lang.pythonã§ã®ä»¥åã®ææ¡ã«ãããã®ã ãšããŸããã
ã¯ã©ã¹å®çŸ©ãšé¢æ°å®çŸ©ã¯æ§æçã«äŒŒãŠããã®ã§ãã¯ã©ã¹è£ 食ã¯æãããªæ¬¡ã®ã¹ãããã®ããã«æããŸãããGuido ã¯ãŸã çŽåŸããŠããããã¯ã©ã¹è£ 食㯠Python 2.4 ã«ã¯ã»ãšãã©ãªãã§ãããã
ãã®è°è«ã¯2002幎2æãã2004幎7æãŸã§python-devã§ç¶ç¶ãããŸãããäœçŸãã®æçš¿ããªããã人ã ã¯å€ãã®å¯èœãªæ§æã®ããªãšãŒã·ã§ã³ãææ¡ããŸãããGuidoã¯ææ¡ã®ãªã¹ããEuroPython 2004ã«æã£ãŠè¡ããããã§è°è«ãè¡ãããŸããããã®åŸã圌ã¯Javaã¹ã¿ã€ã«ã®@decoratoræ§æãæã€ããšã«æ±ºãã2.4a2ã§åããŠç»å ŽããŸãããBarry Warsaw ã¯ããã 'pie-decorator' æ§æãšåä»ããŸãããããã¯ããã³ã¬ãŒã¿æ§æãšåææã«èµ·ãã£ã Pie-thon Parrot shootout ã«ã¡ãªãã§ããããŸã @ ãå°ããã€ã«äŒŒãŠããããã§ããGuido 㯠Python-dev ã§ãåŽäžããã(å€ãã®)ãã©ãŒã ã«é¢ãããã®èšäºãå«ãã圌ã®ã±ãŒã¹ãæŠèª¬ããŸããã
ãã³ã¬ãŒã¿ãŒããšããååã«ã€ããŠ
ãã®ç¹éã«ããã³ã¬ãŒã¿ãŒããšããååãéžãã ããšã«ã€ããŠã¯ãå€ãã®äžæºããããäž»ãªãã®ã¯ããã®ååãGoFããã¯ã§ã®äœ¿ãæ¹ãšäžè²«ããŠããªããšããããšã§ããdecorator'ãšããååã¯ãããããã³ã³ãã€ã©ã®åéã§ã®äœ¿çšãã€ãŸãã·ã³ã¿ãã¯ã¹ããªãŒãæ©ããªãã泚éãã€ããããšã«ç±æ¥ããŠããã®ã ããããã£ãšããååããããããããªãã
èšèšç®æš
æ°ããæ§æã¯
ãŠãŒã¶ãŒå®çŸ©ã® callable ããæ¢åã®çµã¿èŸŒã¿é¢æ° classmethod() ã staticmethod() ãå«ããä»»æã®ã©ãããŒã§åäœããããšããã®èŠä»¶ã¯ããã³ã¬ãŒã¿æ§æãã©ãããŒã³ã³ã¹ãã©ã¯ã¿ãžã®åŒæ°ã®åãæž¡ãããµããŒãããªããã°ãªããªãããšãæå³ããŸãã
å®çŸ©ããšã«è€æ°ã®ã©ãããŒãæ±ãããšãã§ããŸãã
äœãèµ·ãã£ãŠããã®ããäžç®çç¶ã§ããããšãå°ãªããšããæ°ãããŠãŒã¶ãŒãèªåã®ã³ãŒããæžããšãã«ããããç¡èŠããŠã倧äžå€«ãªããã«äžç®çç¶ã§ããããšã
äžåºŠèª¬æããã°...èŠãããããæ§æã§ããããšã
å°æ¥ã®æ¡åŒµãé£ããããªã
å
¥åãç°¡åã§ããããšããã®æ§æã䜿ãããã°ã©ã ã¯ãéåžžã«é »ç¹ã«ãã®æ§æã䜿ãããšãäºæ³ãããã
ã³ãŒããçŽ æ©ãã¹ãã£ã³ããããšãé£ããããªãããšããã¹ãŠã®å®çŸ©ãç¹å®ã®å®çŸ©ããããã¯é¢æ°ãåãä»ããåŒæ°ãæ€çŽ¢ããã®ã¯ãäŸç¶ãšããŠç°¡åã§ãªããã°ãªããªãã
èšèªäŸåã®ãšãã£ã¿ãŒãä»ã® "ããã¡ãã®ããŒãµãŒããŒã« "ã®ãããªäºæ¬¡çãªãµããŒãããŒã«ãäžå¿
èŠã«è€éã«ããªãã
å°æ¥ã®ã³ã³ãã€ã©ããã³ã¬ãŒã¿ãæé©åã§ããããã«ãããPythonã®JITã³ã³ãã€ã©ããããç»å ŽããããšãæåŸ
ãããšããã³ã¬ãŒã¿ã®æ§æãé¢æ°å®çŸ©ã®åã«çœ®ãå¿
èŠããããŸãã
çŸåšã¯é ãããŠããé¢æ°ã®æ«å°Ÿããããã£ãšç®ã«ã€ããããåæ¹ãžç§»åããŸãã
Andrew Kuchlingæ°ã®ããã°ã«ã¯ãåæ©ãšäœ¿çšäŸã«é¢ããå€ãã®è°è«ãžã®ãªã³ã¯ããããç¹ã«æ³šç®ãã¹ãã¯Jim Hugininã®ãŠãŒã¹ã±ãŒã¹ã®ãªã¹ãã ã
Python 2.4a2ã§å®è£ ãããŠããé¢æ°ãã³ã¬ãŒã¿ãŒã®çŸåšã®æ§æã¯ä»¥äžã®éãã§ãïŒ
@dec2
@dec1
def func(arg1, arg2, ...):
pass
ã¯ãã ããã次ãšç䟡ã§ã
def func(arg1, arg2, ...):
pass
func = dec2(dec1(func))
ãå€æ°funcã«ä»£å ¥ããå¿ èŠã¯ãªãããã³ã¬ãŒã¿ãŒã¯é¢æ°å®£èšã®è¿ãã«ãããèšå·ã¯ãããã§äœãæ°ããããšãèµ·ãã£ãŠããããšãæããã«ããŠããã
é©çšé åºïŒäžããäžïŒã®æ ¹æ ã¯ãé¢æ°é©çšã®éåžžã®é åºãšäžèŽããŠããããã§ãããæ°åŠã§ã¯ãé¢æ°ã®åæ(g o f)(x)ã¯g(f(x))ãšèš³ããããPythonã§ã¯ã@g @f def foo()ã¯foo=g(f(foo))ãšãªãã
ãã³ã¬ãŒã¿ãŒã»ã¹ããŒãã¡ã³ããåãå ¥ããããšãã§ãããã®ã¯éãããŠãããä»»æã®åŒã¯æ©èœããªããã°ã€ãã¯çŽæçã«ããã奜ãã ã
çŸåšã®æ§æã§ã¯ããã³ã¬ãŒã¿ãŒå®£èšã§ãã³ã¬ãŒã¿ãŒãè¿ãé¢æ°ãåŒã³åºãããšãã§ããïŒ
@decomaker(argA, argB, ...)
def func(arg1, arg2, ...):
pass
ã¯ãã ããã次ãšç䟡ã§ã
func = decomaker(argA, argB, ...)(func)
ãã³ã¬ãŒã¿ãŒãè¿ãé¢æ°ãæã€æ ¹æ ã¯ã@èšå·ã®åŸãåŒãšã¿ãªãããšãã§ãïŒæ§æçã«ã¯åãªãé¢æ°ã«éå®ããããïŒããã®åŒãè¿ããã®ã¯äœã§ãåŒã³åºãããããã§ããã宣èšåŒæ°ãåç §ã
ãªã@ãªã®ãïŒ
Javaã§ã¯ãæåã¯Javadocã³ã¡ã³ãã®ç®å°ãšããŠããã®åŸJava 1.5ã§Pythonã®ãã³ã¬ãŒã¿ãŒã«äŒŒãã¢ãããŒã·ã§ã³ã«@ã䜿ã£ãæŽå²ããããPythonã§@ãããŒã¯ã³ãšããŠäœ¿ãããŠããªãã£ããšããããšã¯ããã®ãããªã³ãŒãã以åã®ããŒãžã§ã³ã®Pythonã§è§£æãããå¯èœæ§ããªããšããããšã§ãããã埮åŠãªæå³äžã®ãã°ã«ã€ãªããå¯èœæ§ããããŸãããŸããäœããã³ã¬ãŒã¿ãŒã§äœããã³ã¬ãŒã¿ãŒã§ãªãããšããææ§ããåãé€ãããããšãæå³ããŸãããšã¯ããã@ã¯ãŸã ããªãæ£æçãªéžæã§ãã代ããã«ïœã䜿ãããšãææ¡ãã人ãããã
ãã³ã¬ãŒã¿ãæå®ããããã«ãªã¹ãã®ãããªæ§æã䜿ãæ§æãªãã·ã§ã³ã«ã€ããŠã¯ã ããã€ãã®éžæè¢ãææ¡ãããïŒ [...|]ã[...]ããããŠ<...>ã§ããã
Pythonã®ãã³ã¬ãŒã¿ã¯é¢æ°åããã°ã©ãã³ã°ã®ã³ã³ã»ãããšããã€ãã®é¡äŒŒç¹ããããŸããå ·äœçã«ã¯ä»¥äžã®ç¹ãæããããŸãã
é«éé¢æ° (Higher-Order Functions):
ãã³ã¬ãŒã¿ã¯é¢æ°ãåŒæ°ãšããŠåãåããæ°ããé¢æ°ãè¿ãé«éé¢æ°ã®äžçš®ã§ããé¢æ°åããã°ã©ãã³ã°ã§ããé«éé¢æ°ã¯åºæ¬çãªæŠå¿µã®äžã€ã§ããé¢æ°ã®åæ (Function Composition):
ãã³ã¬ãŒã¿ã䜿çšãããšãé¢æ°ã®ååŸã«è¿œå ã®åŠçãçµã¿èŸŒãããšãã§ããŸããããã¯é¢æ°ãåæããããšã§æ°ããæ©èœãæããããé¢æ°åããã°ã©ãã³ã°ã®æŠå¿µã«é¡äŒŒããŠããŸããã€ãã¥ãŒã¿ããªã㣠(Immutability):
ãã³ã¬ãŒã¿ã¯æ¢åã®é¢æ°ãå€æŽããã®ã§ã¯ãªããæ°ããé¢æ°ãäœæããŸããããã¯é¢æ°åããã°ã©ãã³ã°ã§éèŠãããã€ãã¥ãŒã¿ããªãã£ã®èãæ¹ã«åèŽããŸãããã³ã¬ãŒã¿ãã§ãŒã³ (Decorator Chain):
è€æ°ã®ãã³ã¬ãŒã¿ããã§ãŒã³ããããšã§ãé¢æ°ã«å¯ŸããŠè€æ°ã®ä¿®é£Ÿãé©çšã§ããŸãããããé¢æ°åããã°ã©ãã³ã°ã®é¢æ°åæã®èãæ¹ã«äŒŒãŠããŸãã
ãããã®ç¹ãããPythonã®ãã³ã¬ãŒã¿ã¯é¢æ°åããã°ã©ãã³ã°ã®ã³ã³ã»ãããšå¯æ¥ã«é¢é£ããŠãããšèšããŸãã
ãé¡ãèŽããŸã