[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 lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable.
There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various man ......
¡¡¡¡PythonÖпÉÒÔʹÓÃ×°ÊÎÆ÷¶Ôº¯Êý½øÐÐ×°ÊΣ¨»ò˵°ü×°£©£¬ÀûÓÃÕâ¸öÌØÐÔ£¬¿ÉÒԺܷ½±ã¡¢¼ò½àµØ½â¾öһЩÎÊÌ⣬±ÈÈç»ñµÃº¯ÊýÖ´ÐÐʱ¼äµÄÎÊÌâ¡£
¡¡¡¡Ê×ÏÈ£¬ÎÒÃǶ¨ÒåÒ»¸öº¯Êý£¬ÈçÏ£º
¡¡¡¡def exeTime(func):
def newFunc(*args, **args2):
t0 = time.time()
print "@%s, {%s} start" % (time.strftime("%X", time.local ......
Ä£¿é
Ò».¼ò½é
Ä£¿é»ù±¾ÉϾÍÊÇÒ»¸ö°üº¬ÁËËùÓÐÄ㶨ÒåµÄº¯ÊýºÍ±äÁ¿µÄÎļþ¡£ÎªÁËÔÚÆäËû³ÌÐòÖÐÖØÓÃÄ£¿é£¬Ä£¿éµÄÎļþÃû±ØÐëÒÔ.pyΪÀ©Õ¹Ãû¡£
ÀýÈ磺
#!/usr/bin/python
# Filename: using_sys.py
import sys
print 'The command line arguments are:'
for i in sys.argv:
print i
print '\n ......
ѧϰÁ˵ڰËÕº󣬽ôÂàÃܹĵĿªÊ¼µÚ¾ÅÕµÄѧϰ¡£
ѧϰÁË“Êý¾Ý½á¹¹²Ù×÷”£¬Ñ§Ï°ÁË“Îļþ²Ù×÷”¡£“Êý¾Ý½á¹¹²Ù×÷”»¹±È½ÏÓÐÒâ˼£¬¿´µÃ¶®¡£“Îļþ²Ù×÷”¾Í¿ªÊ¼Í·»è»èµÄ£¬ÏñÄÔ×Ó½øÁ˽¬ºý¡£
ºÃ²»ÈÝÒ׿´µ½“²Ù×÷³ÌÐò”£¬²Å·¢ÏÖ“²Ù×÷³ÌÐò”ÕâÒ»½ÚµÄÄÚÈݲ¢²»ÊÇÎÒËù ......
pythonµÄsmtplibÌṩÁËÒ»Öֺܷ½±ãµÄ;¾¶·¢Ë͵ç×ÓÓʼþ¡£Ëü¶ÔsmtpÐÒé½øÐÐÁ˼òµ¥µÄ·â×°¡£
smtpÐÒéµÄ»ù±¾ÃüÁî°üÀ¨£º
HELO Ïò·þÎñÆ÷±êʶÓû§Éí·Ý
MAIL ³õʼ»¯Óʼþ´«Êä mail from:
&nb ......