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

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

(ת)[Python ѧϰ]2.5°æyield֮ѧϰÐĵÃ
 
ÔÚ shhgs ·¢²¼Á˹ØÓÚ¡¶ Py 2.5 what's new Ö® yield¡·Ö®ºó£¬Ô­À´ÎÒ²»ÊÇÌØ±ð¹Ø×¢ yield µÄÓ÷¨£¬ÒòΪ¶ÔÓÚ2.3ÖмÓÈëµÄyieldÏà¶ÔÀ´Ëµ¹¦Äܼòµ¥£¬ËüÊÇ×÷Ϊһ¸ö generator ²»¿ÉȱÉÙµÄÒ»ÌõÓï¾ä£¬Ö»Òª°üº¬ËüµÄº¯Êý¼´ÊÇÒ»¸ö generator ¡£µ«ÔÚ2.3ÖУ¬generator ²»ÄÜÖØÈ룬²»ÄÜÔÚÔËÐйý³ÌÖÐÐ޸쬲»ÄÜÒý·¢Òì³££¬ÄãҪôÊÇ˳Ðòµ÷Óã¬ÒªÃ´¾Í´´½¨Ò»¸öÐ嵀 generator¡£¶øÇÒ generator ÖÐµÄ yield Ö»ÊÇÒ»¸öÓï¾ä¡£µ«µ½ÁË 2.5 °æÖ®ºó£¬Çé¿ö·¢ÉúÁ˺ÜÔڵı仯¡£
ÔÚ shhgs µÄÎÄÕÂÖжÔÓÚ yield ²¢Ã»ÓÐ×öÌ«¶àµÄÃèÊö£¬Ò²Òò´ËÈÃÎÒÔÚÀí½âÉϲúÉúÁËÐí¶àÎÊÌ⣬ÓÚÊÇÎÒ×ÐϸµØÑо¿ÁË What's new ºÍ PEP 342 Îĵµ£¬ÓÐÁËһЩÌå»á£¬ÃèÊöÔÚÏÂÃæ¡£
ÕâÀﲻ˵ΪʲôҪ¶Ô yield ½øÐÐÐ޸ģ¬Ö»Ëµ¹¦ÄÜ¡£
1. yield ³ÉΪÁ˱í´ïʽ£¬Ëü²»ÔÙÊÇÓï¾ä£¬µ«¿ÉÒÔ·ÅÔÚµ¥¶ÀµÄÐÐÉÏ¡£Ô­ÎÄ£º
Redefine "yield" to be an expression, rather than a statement. The current yield statement would become a yield expression whose value is thrown away.
¿ÉÒÔ¿´µ½£¬Èç¹ûÄ㻹ÊÇд³ÉÓï¾äÐÎʽµÄ»°£¬Æäʵ»¹ÊÇÒ»¸ö±í´ïʽ£¬Ö»ÊÇËüµÄÖµ±»ÈÓµôÁË¡£
ÄÇôһ¸ö yield ±í´ïʽ¿ÉÒÔÕâÑùд£º
x = yield i
y = x + (yield x)
ÄÇôÕâÖÖ»úÖÆµ½µ×ÊÇÈçºÎ¹¤×÷µÄÄØ£¿ÔÚ2.3°æºÜÈÝÒ×Àí½â£¬ÄãÍêÈ«¿ÉÒÔ°Ñ yield Óï¾äÀí½âΪһ¸ö "return" Óï¾ä£¬Ö»²»¹ý "return" Íêºó£¬º¯Êý²¢²»½áÊø£¬¶øÊǶÏÐøÔËÐУ¬Ö±µ½ÔÙ´ÎÓöµ½ yield Óï¾ä¡£ÄÇôµ½ÁË 2.5 °æ²»½ö½öÊÇÒ»¸ö "return" Óï¾äÄÇô¼òµ¥ÁË£¬ÈÃÎÒÃÇ¿´ÍêÏÂÃæ¹ØÓÚ send() µÄ˵Ã÷ÔÙÃèÊöËü°É¡£
2. Ôö¼ÓÁË send(msg) ·½·¨£¬Òò´ËÄã¿ÉÒÔʹÓÃËüÏò generator ·¢ËÍÏûÏ¢¡£Ô­ÎÄ£º
Add a new send() method for generator-iterators, which resumes the generator and "sends" a value that becomes the result of the current yield-expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value.
Ö´ÐÐÒ»¸ö send(msg) »á»Ö¸´ generator µÄÔËÐУ¬È»ºó·¢Ë͵ÄÖµ½«³ÉΪµ±Ç° yield ±í´ïʽµÄ·µ»ØÖµ¡£È»ºó send() »á·µ»ØÏÂÒ»¸ö±» generator yield µÄÖµ£¬Èç¹ûûÓÐÏÂÒ»¸ö¿ÉÒÔ yield µÄÖµÔòÒý·¢Ò»¸öÒì³£¡£
ÄÇô¿ÉÒÔ¿´¹ýÕâÆäʵ°üº¬ÁËÒ»´ÎÔËÐУ¬´Ó½«msg¸³¸øµ±Ç°±»Í£×¡µÄ yield ±í´ïʽ¿ªÊ¼£¬µ½ÏÂÒ»¸ö yield Óï¾ä½áÊø£¬È»ºó·µ»ØÏÂÒ»¸öyieldÓï¾äµÄ²ÎÊý£¬È»ºóÔÙ¹ÒÆ


Ïà¹ØÎĵµ£º

Python: ʹÓÃ×°ÊÎÆ÷¡°@¡±È¡µÃº¯ÊýÖ´ÐÐʱ¼ä

¡¡¡¡PythonÖпÉÒÔʹÓÃ×°ÊÎÆ÷¶Ôº¯Êý½øÐÐ×°ÊΣ¨»ò˵°ü×°£©£¬ÀûÓÃÕâ¸öÌØÐÔ£¬¿ÉÒԺܷ½±ã¡¢¼ò½àµØ½â¾öһЩÎÊÌ⣬±ÈÈç»ñµÃº¯ÊýÖ´ÐÐʱ¼äµÄÎÊÌâ¡£
¡¡¡¡Ê×ÏÈ£¬ÎÒÃǶ¨ÒåÒ»¸öº¯Êý£¬ÈçÏ£º
¡¡¡¡def exeTime(func):
def newFunc(*args, **args2):
t0 = time.time()
print "@%s, {%s} start" % (time.strftime("%X", time.local ......

python PIL ÅúÁ¿´¦Àí´¦ÀíͼƬ

¿Í»§¸øÒ»¶ÑͼƬҪ´«µ½ºǫ́£¬Í¼Æ¬Ì«´óÁË£¬ÉϰÙÕÅͼÓÃphotoshop¸ÄÌ«Âý£¬¾ÍÏëµ½ÓÃpythonд¸ö¼òµ¥µÄÅú´¦Àí¡£¹¦Äܼòµ¥¾ÍÊǰÑԭͼ°´±ÈÀýËõС£¬´úÂë¸ü¼òµ¥ 20¶àÐС£
# -*- coding: cp936 -*-
import Image
import glob, os
#ͼƬÅú´¦Àí
def timage():
for files in glob.glob('D:\\1\\*.JPG'):
filepath,filena ......

PythonÖÐÎÄÈ«¹¥ÂÔ

http://blog.chinaunix.net/u1/59571/showart_1901962.html
1.        ÔÚPythonÖÐʹÓÃÖÐÎÄ
ÔÚPythonÖÐÓÐÁ½ÖÖĬÈϵÄ×Ö·û´®£ºstrºÍunicode¡£ÔÚPythonÖÐÒ»¶¨Òª×¢ÒâÇø·Ö“Unicode×Ö·û´®”ºÍ“unicode¶ÔÏó”µÄÇø±ð¡£ºóÃæËùÓеēunicode×Ö·û´®”Ö¸µÄ¶¼ÊÇpyt ......

PythonÍêÈ«ÐÂÊֽ̳Ì

http://www.cppblog.com/oosky/archive/2005/10/11/639.html
Lesson 1 ×¼±¸ºÃѧϰPythonµÄ»·¾³
Python µÄ¹Ù·½ÍøÖ·£º
www.python.org
µã»÷ÏÂÃæÁ¬½Ó¾Í¿ÉÒÔÖ±½ÓÏÂÔØÁË£¬ÕâÀïÖ»ÌṩÁËWindowsϵÄPython¡£
http://www.python.org/ftp/python/2.4.2/python-2.4.2.msi
linux°æ±¾µÄÎҾͲ»ËµÁË£¬ÒòΪÈç¹ûÄãÄܹ»Ê¹ÓÃlinux²¢°²×° ......

pythonÁ½¸ödictÏà¼Ó


>>> a = {'1':'2'}
>>> b = {'3':'4'}
>>> a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>> a.update(b)
>>> a
{'1': '2', '3': '4'} ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ