Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

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֮ȫ¾Ö±äÁ¿

Ó¦¸Ã¾¡Á¿±ÜÃâʹÓÃÈ«¾Ö±äÁ¿¡£²»Í¬µÄÄ£¿é¶¼¿ÉÒÔ×ÔÓɵķÃÎÊÈ«¾Ö±äÁ¿£¬¿ÉÄܻᵼÖÂÈ«¾Ö±äÁ¿µÄ²»¿ÉÔ¤ÖªÐÔ¡£¶ÔÈ«¾Ö±äÁ¿£¬Èç¹û³ÌÐòÔ±¼×ÐÞ¸ÄÁË_aµÄÖµ£¬³ÌÐòÔ±ÒÒͬʱҲҪʹÓÃ_a£¬Õâʱ¿ÉÄܵ¼Ö³ÌÐòÖеĴíÎó¡£ÕâÖÖ´íÎóÊǺÜÄÑ·¢Ïֺ͸üÕýµÄ¡£ 
È«¾Ö±äÁ¿½µµÍÁ˺¯Êý»òÄ£¿éÖ®¼äµÄͨÓÃÐÔ£¬²»Í¬µÄº¯Êý»òÄ£¿é¶¼ÒªÒÀÀµÓÚÈ«¾Ö±äÁ¿¡£Í¬Ñù£¬È« ......

Andorid»·¾³Python±à³Ì

 http://code.google.com/p/android-scripting/
Android¿ªÊ¼Ö§³Ö½Å±¾ÓïÑÔPython¡¢Lua¼°Beanshell£¬Î´À´»¹½«Ö§³ÖRuby
×÷Õß Werner Schuster ÒëÕß ÕÅÁú ·¢²¼ÓÚ 2009Äê7ÔÂ5ÈÕ
Android Scripting Environment£¨ASE£©ÏîĿΪAndroidÔö¼ÓÁ˽ű¾Ö§³Ö¡£Í¨¹ý°²×°ASE£¬ÎÒÃÇ¿ÉÒÔÔÚÉ豸Éϱàд½Å±¾²¢Ö´ÐС£
ËäÈ»ÏÖÔÚÒѾ­Ó¿ÏÖ³öÁ ......

python pyc

 Ê²Ã´ÊÇpycÎļþ
pycÊÇÒ»ÖÖ¶þ½øÖÆÎļþ£¬ÊÇÓÉpyÎļþ¾­¹ý±àÒëºó£¬Éú³ÉµÄÎļþ£¬ÊÇÒ»ÖÖbyte code£¬pyÎļþ±ä³ÉpycÎļþºó£¬¼ÓÔصÄËÙ¶ÈÓÐËùÌá¸ß£¬¶øÇÒpycÊÇÒ»ÖÖ¿çƽ̨µÄ×Ö½ÚÂ룬ÊÇÓÉpythonµÄÐéÄâ»úÀ´Ö´Ðеģ¬Õâ¸öÊÇÀàËÆÓÚJAVA»òÕß.NETµÄÐéÄâ»úµÄ¸ÅÄî¡£pycµÄÄÚÈÝ£¬ÊǸúpythonµÄ°æ±¾Ïà¹ØµÄ£¬²»Í¬°æ±¾±àÒëºóµÄpycÎļþÊDz»Í¬µÄ£¬2 ......

ÓÃpythonµÄcmdÄ£¿éдһ¸ö¼òµ¥µÄshell

 from cmd import *
class MyShell(Cmd):
    def preloop(self):
        print "print this line before entering the loop"
            
    def postloop(self):
&nb ......

[Python ѧϰ]2.5°æyield֮ѧϰÐĵÃ

  shhgs ·¢²¼Á˹ØÓÚ¡¶ Py 2.5 what's new Ö® yield
¡·
Ö®ºó£¬Ô­À´ÎÒ²»ÊÇÌرð¹Ø×¢ yield µÄÓ÷¨£¬ÒòΪ¶ÔÓÚ2.3ÖмÓÈëµÄyieldÏà¶ÔÀ´Ëµ¹¦Äܼòµ¥£¬ËüÊÇ×÷Ϊһ¸ö generator
²»¿ÉȱÉÙµÄÒ»ÌõÓï¾ä£¬Ö»Òª°üº¬ËüµÄº¯Êý¼´ÊÇÒ»¸ö generator ¡£µ«ÔÚ2.3ÖУ¬generator
²»ÄÜÖØÈ룬²»ÄÜÔÚÔËÐйý³ÌÖÐÐ޸ģ¬²»ÄÜÒý·¢Òì³££¬ÄãҪôÊÇ˳Ðò ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ