Python×°ÊÎÆ÷
ÔÎÄ£ºhttp://www.klipdas.com/blog/?p=python-decorator
python×°ÊÎÆ÷½éÉÜ
Python 2.2ÖÐÒýÈëµÄ classmethod() ºÍ staticmethod() ÄÚÖú¯Êý£¬Äã¿ÉÒÔÕâÑùµ÷ÓÃclassmethod()£º
class A:
def foo(self, y):
print y
foo = classmethod(foo)
Ò²¿ÉÒÔÕâÑù£º
class A:
@classmethod
def foo(self, y):
print y
ËäÈ»classmethodÊÇÄÚÖú¯Êý£¬µ«ËüºÍÆÕͨµÄº¯ÊýûÓÐÁ½Ñù£¬Òò´ËÄãÒ²¿ÉÒÔ´´½¨×Ô¼ºµÄº¯Êýת»¯Âß¼£¬È磺
def mydecorator(func):
print 'mydecorator was called.'
func.name = 'myfun'
return func
¸ÃÐÞÊÎÆ÷½«ÉÔ΢ÐÞ¸ÄÁËÔº¯ÊýµÄÂß¼£ºÊ×ÏÈ´òÓ¡Ò»¶ÎÎÄ×Ö“mydecorator was called.”£¬È»ºó¸øÔº¯ÊýÌí¼ÓÁËÒ»¸önameÊôÐÔ£¬ÊôÐÔֵΪ"myfun"£¬Ö®ºóÔÙ½«º¯Êý·Å»Ø£¬Ê¹Ó÷½·¨ºÍµ÷Óýá¹ûÈçÏ£º
class A:
@mydecorator
def foo(self, y):
print y
if __name__ == '__main__':
a = A()
a.foo(1)
ÔËÐнá¹ûΪ£º
mydecorator was called.
1
ÒÔÉϼò½éÎÒÃÇ¿ÉÒÔ´óÖÂÁ˽âÁËpython×°ÊÎÆ÷µÄ×÷Óá£
×°ÊÎÆ÷µÄµ÷ÓÃÂß¼
ÈçÉÏÒ»½ÚµÄʾÀýÖÐÎÒÃÇ·¢ÏÖ£¬Ê¹ÓÃ×°ÊÎÆ÷µÄʱºòÊÇ@mydecorator£¬»òÕßÊÇfoo=mydecorator(foo)£¬¶ø²»ÊÇ@mydecorator()£¬»òÕßfoo=mydecorator(foo)()¡£ÏÔÈ»£¬ÎÒÃÇÒªµÄ×°ÊÎÆ÷ÊǸöº¯Êý£¬¶ø²»ÊǺ¯ÊýµÄ½á¹û¡£ÒòΪÎÒÃÇÒª½«ÎÒÃÇÒªÐÞÊεĺ¯Êýfoo£¨¼´ÀϺ¯Êý£©´«µÝ¸øÐÞÊκ¯Êýmydecorator£¬²¢ÇÒÐÞÊκ¯ÊýmydecoratorÒ²·µ»ØÒ»¸öк¯Êý£¨ÉÏÒ»µÄк¯ÊýÊÇÔÚÀϺ¯ÊýµÄ»ù´¡ÉÏÌí¼ÓÁËÒ»¸öÊôÐÔ£©¡£
´ø²ÎÊý×°ÊÎÆ÷
ÉÏÒ»½ÚµÄ½éÉÜ×°ÊÎÆ÷µÄµ÷Óùý³Ì£¬ÎÒÃÇ¿ÉÒÔÃ÷°×£¬@mydecoratorÊǸöº¯Êý£¬¶øÇÒÊǸöº¯Êý¾Í¿ÉÒÔ£¬Êµ¼ÊÉÏÊDZØÐëҪʵÏÖ__call__·½·¨µÄ¶ÔÏó¾Í¿ÉÒÔÁË¡£Ò²¾ÍÊÇ˵װÊÎÆ÷¿ÉÒÔ¶¨Òå³ÉʵÏÖÁË__call__·½·¨µÄclassÒ²¿ÉÒÔÊÇÆäËûµÄÈκηµ»Øº¯ÊýµÄ“¶ÔÏ󔣬ºóÕßʵÏֽϸ´ÔÓ£¬µ«¹¦ÄÜÇ¿´ó¡£
ʵÏÖ__call__·½·¨µÄclassʾÀý£º
class mydecorator:
def __init__(self, func):
self.func = func
def __call__(self):
print “mydecorator was called.”
return self.func
ʹÓ÷½·¨Í¬Ç°ÃæµÄʾÀýÒ»Ñù£¬ÒòΪÎÒÃÇ¿ÉÒÔÏñº¯ÊýÒ»Ñùµ÷ÓøÃÀࣺmydecorator()£¬²»Í¬µÄÊÇ£¬¸ÃÀàÊÇÒÔ±»×°Êεĺ¯Êý×÷Ϊ¹¹Ôì²ÎÊý£¬¶ø²»ÊÇ×÷Ϊ__call__µÄ²ÎÊý¡£¸ü¸´ÔÓµÄ×°ÊÎÆ÷£º
def mydecorator(arg):
print “arg is ”, arg
def newdec(func):
print ‘new decor
Ïà¹ØÎĵµ£º
Python»ù±¾°²×°£º
* http://www.python.org/ ¹Ù·½±ê×¼Python¿ª·¢°üºÍÖ§³Ö»·¾³£¬Í¬Ê±Ò²ÊÇPythonµÄ¹Ù·½ÍøÕ¾£»
* http://www.activestate.com/ ¼¯³É¶à¸öÓÐÓòå¼þµÄÇ¿´ó·Ç¹Ù·½°æ±¾£¬ÌرðÊÇÕë¶ÔWindows»·¾³Óв»ÉٸĽø£»
PythonÎĵµ£º
* http://www.pyth ......
Ctrl+3 ÐÐ×¢ÊÍ
Ctr+\ È¥ÐÐ×¢ÊÍ
Ctrl+Shift+3 È¥ÐÐ×¢ÊÍ
Ctrl+4 ¿é×¢ÊÍ
Ctrl+5 & ......
ת×Ô:http://blog.chinaunix.net/u3/103146/showart_2058891.html
PythonÕâÖֽű¾ÓïÑÔµÄÇ¿´ó¹¦ÄÜÔ½À´Ô½±»¹ã´óµÄ³ÌÐòÔ±ËùÖØÊÓ£¬ÕâÖÖ֮ǰÔÚ¹úÄÚÁ÷ÐжȲ»¸ßµÄÓïÑÔ½üÀ´ÆøÊƸßÕÇ¡£¸÷ÖÖµÚÈý·½Ä£¿é²ã³ö²»Çî¡£
±¾ÎĽéÉܵıãÊÇÒ»ÖÖÄܷdz£·½±ã²Ù×÷IEµÄµÚÈý·½¹¤¾ß£¬PAMIE£¬ËûÄÜÈÃÄãÈçͬдJSÒ»ÑùÀ´²Ù×÷IEä¯ÀÀÆ÷¡£°üÀ¨×Ô ......
http://code.google.com/p/android-scripting/
Android¿ªÊ¼Ö§³Ö½Å±¾ÓïÑÔPython¡¢Lua¼°Beanshell£¬Î´À´»¹½«Ö§³ÖRuby
×÷Õß Werner Schuster ÒëÕß ÕÅÁú ·¢²¼ÓÚ 2009Äê7ÔÂ5ÈÕ
Android Scripting Environment£¨ASE£©ÏîĿΪAndroidÔö¼ÓÁ˽ű¾Ö§³Ö¡£Í¨¹ý°²×°ASE£¬ÎÒÃÇ¿ÉÒÔÔÚÉ豸Éϱàд½Å±¾²¢Ö´ÐС£
ËäÈ»ÏÖÔÚÒѾӿÏÖ³öÁ ......
Python , Delphi , Loki Èý¸öµ¥´ÊÓÐȤµÄÀ´Ô´
Ðí¶à³ÌÐòÉè¼ÆÓïÑԺͿª·¢¹¤¾ßÒÔ¼°¿âµÄÃû×Ö¶¼À´Ô´ÓëÉñ»°£¬ÆÄÓÐÒâ˼£º
°¢²¨ÂÞÊÇͬʱ³öÏÖÔÚÏ£À°ºÍÂÞÂíÉñ»°ÖеİÂÁÖÅÁ˹ʮ¶þ´óÉñÖ®Ò»¡£Ëû³ýÁËÓµÓÐÌ«ÑôÉñµÄÉí·Ö£¬Í¬Ê±»¹ÓÐÔ¤ÑÔ¡¢ÒÕÊõ¡¢Ò½ÊõÖ®ÉñµÄ³ÆºÅ¡£°¢²¨ÂÞÊÇÖæ˹ºÍÀÖ¶äµÄ¶ù×Ó£¬ºÍ°¢µÙÃÛ˹ÊÇË ......