PythonºÍDecorator£¨×°ÊÎÆ÷£©Ä£Ê½
Ïȸø³öÒ»¸öËÄÈËÍŶÔ
Decorator mode
µÄ¶¨Ò壺
¶¯Ì¬µØ
¸øÒ»¸ö
¶ÔÏó
Ìí¼ÓһЩ
¶îÍâµÄÖ°Ôð
¡£
ÔÙÀ´ËµËµÕâ¸öģʽµÄºÃ´¦£ºÈÏÖ¤£¬È¨ÏÞ¼ì²é£¬¼ÇÈÕÖ¾£¬¼ì²é²ÎÊý£¬¼ÓËø£¬µÈµÈµÈµÈ£¬ÕâЩ¹¦ÄܺÍϵͳҵÎñÎ޹أ¬µ«ÓÖÊÇϵͳËù±ØÐëµÄ£¬ËµµÄ¸üÃ÷°×Ò»µã£¬¾ÍÊÇÃæÏò·½ÃæµÄ±à³Ì£¨
AOP
£©¡£
AOP
°ÑÓëÒµÎñÎ޹صĴúÂëÊ®·Ö¸É¾»µÄ´ÓϵͳÖÐÇиî³öÀ´£¬µ«ÊÇ
Decorator mode
µÄÇ¿´óÔ¶²»Ö¹ÓÚ´Ë£¬±¾ÎĵÄÖصãÔÚÓÚ
Decorator mode
ÔÚ
Python
ÖеÄÓ¦Óã¬ËùÒԾͲ»ÔÙ¹ý¶àÃèÊö
Decorator mode
±¾ÉíÁË£¬ÒªÏëÉîÈëÁ˽â¸Ãģʽ£¬Çë²Î¿¼ËÄÈËÍŵľµäÖ®×÷¡¶Éè¼Æģʽ¡·¡£
ÔÚ
Python
ÖÐ
Decorator mode
¿ÉÒÔ°´ÕÕÏñÆäËü±à³ÌÓïÑÔÈç
C++, Java
µÈµÄÑù×ÓÀ´ÊµÏÖ£¬µ«ÊÇ
Python
ÔÚÓ¦ÓÃ
×°ÊÎ
¸ÅÄî·½ÃæµÄÄÜÁ¦ÉÏÔ¶²»Ö¹ÓÚ´Ë£¬
Python
ÌṩÁËÒ»¸öÓï·¨ºÍÒ»¸ö±à³ÌÌØÐÔÀ´¼ÓÇ¿Õâ·½ÃæµÄ¹¦ÄÜ¡£
Python
ÌṩµÄÓï·¨¾ÍÊÇ×°ÊÎÆ÷Óï·¨
(decorator)
£¬ÈçÏ£º
@aoo
def
foo(): pass
def
aoo(fn):
return fn
ÕâÀï²»¶Ô×°ÊÎÆ÷Óï·¨×ö¹ý¶àµÄ½âÊÍ£¬ÒòΪװÊÎÆ÷Óï·¨Ò²ÊÇ»ùÓÚÎÒ½«Òª½éÉܵÄÁíÒ»¸ö±à³ÌÌØÐÔ£¬µ±ÎÒ½éÉÜÍêÁíÒ»¸ö±à³ÌÌØÐÔºó£¬ÏàÐÅÄã»á¶Ô×°ÊÎÆ÷Óï·¨ÓиüÉîÈëµÄÈÏʶ¡£
Ò»¸öÊ®·ÖÖØÒªµÄ±à³ÌÌØÐÔ“±Õ°ü”
(closure)
¡Öصdz¡£¨ÌâÍâ»°£º¾Ý˵“±Õ°ü”ÒѾ½øÈë
java
ÏÂÒ»°æµÄÌØÐÔºòÑ¡ÁбíÁË£©
ÔÚ
Python
£¬
PHP
£¬
Perl
£¬
Ruby
£¬
JavaScript
µÈ¶¯Ì¬ÓïÑÔÖУ¬¶¼ÒѾʵÏÖÁ˱հüÌØÐÔ£¬
ΪʲôÕâ¸öÌØÐÔÄÇôÖØÒªÄØ£¿ÎÒÃÇÏÈÀ´¿´¿´ËüµÄͨË×һЩµÄ¶¨Ò壺
OO±à³Ì·¶Ê½ÖеĶÔÏóÊÇ“ÕûºÏÁ˺¯ÊýµÄÊý¾Ý¶ÔÏ󔣬ÄÇô±Õ°ü¾ÍÊÇ“ÕûºÏÁËÊý¾ÝµÄº¯Êý¶ÔÏó”
Ô´Îĵµ <http://blog.csdn.net/muzisoft2008/archive/2009/03/30/4036744.aspx
>
½èÓÃÒ»¸ö·Ç³£ºÃµÄ˵·¨À´×ö¸ö×ܽá(×¢
4
)£º¶ÔÏóÊǸ½ÓÐÐÐΪµÄÊý¾Ý£¬¶ø±Õ°üÊǸ½ÓÐÊý¾ÝµÄÐÐΪ¡£
Ô´Îĵµ <http://www.ibm.com/developerworks/cn/linux/l-cn-closure/index.html
>
ÕâƪÎÄÕ¸üÏêϸµÄ½éÉÜÁ˱հü
±Õ°üµÄ¸ÅÄî¡¢ÐÎʽÓëÓ¦ÓÃ
Ô´Îĵµ <http://www.ibm.com/developerworks/cn/linux/l-cn-closure/index.html
>
ÏÂÃæ×ó±ßµÄ
foo1
Ö»ÊÇÒ»¸öÆÕͨµÄÄÚǶº¯Êý£¬¶øÓұߵÄ
boo
ÔòÊÇÒ»¸ö±Õ°ü£¬
def
foo(x):
&
Ïà¹ØÎĵµ£º
¼ò½é
ÄãÒѾѧϰÁËÈçºÎÔÚÄãµÄ³ÌÐòÖж¨ÒåÒ»´Îº¯Êý¶øÖØÓôúÂë¡£Èç¹ûÄãÏëÒªÔÚÆäËû³ÌÐòÖÐÖØÓúܶຯÊý£¬ÄÇôÄã¸ÃÈçºÎ±àд³ÌÐòÄØ£¿Äã¿ÉÄÜÒѾ²Âµ½ÁË£¬´ð°¸ÊÇʹÓÃÄ£¿é¡£Ä£¿é»ù±¾ÉϾÍÊÇÒ»¸ö°üº¬ÁËËùÓÐÄ㶨ÒåµÄº¯ÊýºÍ±äÁ¿µÄÎļþ¡£ÎªÁËÔÚÆäËû³ÌÐòÖÐÖØÓÃÄ£¿é£¬Ä£¿éµÄÎļþÃû±ØÐëÒÔ.pyΪÀ©Õ¹Ãû¡£
Ä£¿é¿ÉÒÔ´ÓÆäËû³ÌÐò ÊäÈë ÒÔ±ãÀûÓÃËüµ ......
ptyhonºËÐıà³ÌµÚÈý°æµÄ×Ôѧ£¬Ö÷Ҫѧϰ±Ê¼Ç·¢Ï£º
µÚÒ»Õ£ºÊ¹ÓÃÈëÃÅ
ÈËÃÇΪʲôʹÓÃPYTHONÓïÑÔ£º
1¡¢Èí¼þÖÊÁ¿£»
2¡¢¿ª·¢ÕßЧÂÊ£»
3¡¢³ÌÐòµÄ¿ÉÒÆÖ²ÐÔ£»
4¡¢×é¼þ¼¯³É£»
5¡¢ÏíÊÜÀÖȤ£»
PYTHONÓïÑÔµÄȱµã£¬¾ÍÊÇËÙ¶ÈÂý¡£
ʹÓÃPYTHON¿ÉÒÔ×öЩʲô£º
1¡¢ÏµÍ³±à³Ì£»
2¡¢Óû§Í¼Ðνӿڣ»
3¡¢INTERNET½Å±¾£»
4¡¢×é¼þ¼¯³É£ ......
Python Áбí
listÊÇ´¦ÀíÒ»×éÓÐÐòÏîÄ¿µÄÊý¾Ý½á¹¹£¬¼´Äã¿ÉÒÔÔÚÒ»¸öÁбíÖд洢һ¸ö ÐòÁÐ µÄÏîÄ¿¡£¼ÙÏëÄãÓÐÒ»¸ö¹ºÎïÁÐ±í£¬ÉÏÃæ¼ÇÔØ×ÅÄãÒªÂòµÄ¶«Î÷£¬Äã¾ÍÈÝÒ×Àí½âÁбíÁË¡£Ö»²»¹ýÔÚÄãµÄ¹ºÎï±íÉÏ£¬¿ÉÄÜÿÑù¶«Î÷¶¼¶À×ÔÕ¼ÓÐÒ»ÐУ¬¶øÔÚPythonÖУ¬ÄãÔÚÿ¸öÏîÄ¿Ö®¼äÓöººÅ·Ö¸î¡£
ÁбíÖеÄÏîÄ¿Ó¦¸Ã°üÀ¨ÔÚ·½À¨ºÅÖУ¬ÕâÑùPython¾ÍÖªµÀÄãÊ ......
Python Ôª×é
Ôª×éºÍÁбíÊ®·ÖÀàËÆ£¬Ö»²»¹ýÔª×éºÍ×Ö·û´®Ò»ÑùÊÇ ²»¿É±äµÄ ¼´Äã²»ÄÜÐÞ¸ÄÔª×é¡£Ôª×éͨ¹ýÔ²À¨ºÅÖÐÓöººÅ·Ö¸îµÄÏîÄ¿¶¨Òå¡£Ôª×éͨ³£ÓÃÔÚʹÓï¾ä»òÓû§¶¨ÒåµÄº¯ÊýÄܹ»°²È«µØ²ÉÓÃÒ»×éÖµµÄʱºò£¬¼´±»Ê¹ÓõÄÔª×éµÄÖµ²»»á¸Ä±ä¡£
ʹÓÃÔª×é
Àý9.2 ʹÓÃÔª×é
#!/usr/bin/python
# Filename: using_tuple.py
zoo = ('wo ......
µÚÒ»²½£º´î½¨ÊÖ»úÔËÐÐƽ̨
ÒªÔÚS60ÊÖ»úÉÏÔËÐÐPY¿ª·¢µÄÈí¼þ,Ê×ÏȾÍÒª´î½¨ÊÖ»úPYÔËÐл·¾³.ŵ»ùÑÇÊÖ»ú±¾Éí²»Ö§³ÖPython,ËùÒÔÒª°²×°Python²å¼þ.
PS£ºÒ»¶¨Òª°²×°ÔÚCÅÌ£¡ÎÒÒ»¿ªÊ¼×°E£¬½á¹û³öÏÖÁ˺ÜÑÏÖصÄÎÊÌ⣡²»¹ý,×î½üÍøÉÏ·¢ÏÖÓÐEÅÌ°æµÄÀ²,Äã¿ÉÒÔ×Ô¼ºÊÔÑéһϣ¬¹þ¹þ
PythonScriptShell_1_4_5_3rdEd.SIS
Pyth ......