ºìÁªLinuxÃÅ»§
Linux°ïÖú

pythonѧϰÎóÇø×ܽá

·¢²¼Ê±¼ä:2017-09-11 10:43:16À´Ô´:ºìÁª×÷Õß:Ronny
ΪÁËÈóõѧ Python µÄ³ÌÐòÔ±±ÜÃⷸͬÑùµÄ´íÎó£¬ÒÔÏÂÁгöÁËÎÒѧϰ Python ʱ·¸µÄÈýÖÖ´íÎó¡£ÕâЩ´íÎóҪôÊÇÎÒ³¤ÆÚÒÔÀ´¾­³£·¸µÄ£¬ÒªÃ´ÊÇÔì³ÉÁËÐèÒª¼¸¸öСʱ½â¾öµÄÂé·³¡£ ÄêÇáµÄ³ÌÐòÔ±ÃÇ¿ÉҪעÒâÁË£¬ÕâЩ´íÎóÊÇ»áÀË·ÑÒ»ÏÂÎçµÄ£¡
1¡¢ ¿É±äÊý¾ÝÀàÐÍ×÷Ϊº¯Êý¶¨ÒåÖеÄĬÈϲÎÊý
ÕâËƺõÊǶԵģ¿ÄãдÁËÒ»¸öСº¯Êý£¬±ÈÈ磬ËÑË÷µ±Ç°Ò³ÃæÉϵÄÁ´½Ó£¬²¢¿ÉÑ¡½«Æ丽¼Óµ½ÁíÒ»¸öÌṩµÄÁбíÖС£

def search_for_links(page, add_to=[]):
new_links = page.search_for_links()
add_to.extend(new_links)
return add_to
´Ó±íÃæ¿´£¬ÕâÏñÊÇÊ®·ÖÕý³£µÄ Python ´úÂ룬ÊÂʵÉÏËüÒ²ÊÇ£¬¶øÇÒÊÇ¿ÉÒÔÔËÐеġ£µ«ÊÇ£¬ÕâÀïÓиöÎÊÌâ¡£Èç¹ûÎÒÃǸø add_to ²ÎÊýÌṩÁËÒ»¸öÁÐ±í£¬Ëü½«°´ÕÕÎÒÃÇÔ¤ÆÚµÄÄÇÑù¹¤×÷¡£µ«ÊÇ£¬Èç¹ûÎÒÃÇÈÃËüʹÓÃĬÈÏÖµ£¬¾Í»á³öÏÖһЩÉñÆæµÄÊÂÇé¡£
ÊÔÊÔÏÂÃæµÄ´úÂ룺

def fn(var1, var2=[]):
var2.append(var1)
print var2
fn(3)
fn(4)
fn(5)
¿ÉÄÜÄãÈÏΪÎÒÃǽ«¿´µ½£º

[3]
[4]
[5]
µ«Êµ¼ÊÉÏ£¬ÎÒÃÇ¿´µ½µÄÈ´ÊÇ£º

[3]
[3, 4]
[3, 4, 5]
ΪʲôÄØ£¿ÈçÄãËù¼û£¬Ã¿´Î¶¼Ê¹ÓõÄÊÇͬһ¸öÁÐ±í£¬Êä³öΪʲô»áÊÇÕâÑù£¿ÔÚ Python ÖУ¬µ±ÎÒÃDZàдÕâÑùµÄº¯Êýʱ£¬Õâ¸öÁÐ±í±»ÊµÀý»¯Îªº¯Êý¶¨ÒåµÄÒ»²¿·Ö¡£µ±º¯ÊýÔËÐÐʱ£¬Ëü²¢²»ÊÇÿ´Î¶¼±»ÊµÀý»¯¡£ÕâÒâζ×Å£¬Õâ¸öº¯Êý»áһֱʹÓÃÍêÈ«Ò»ÑùµÄÁбí¶ÔÏ󣬳ý·ÇÎÒÃÇÌṩһ¸öеĶÔÏó£º

fn(3, [4])
[4, 3]
´ð°¸ÕýÈçÎÒÃÇËùÏëµÄÄÇÑù¡£ÒªÏëµÃµ½ÕâÖÖ½á¹û£¬ÕýÈ·µÄ·½·¨ÊÇ£º

def fn(var1, var2=None):
if not var2:
var2 = []
var2.append(var1)
»òÊÇÔÚµÚÒ»¸öÀý×ÓÖУº

def search_for_links(page, add_to=None):
if not add_to:
add_to = []
new_links = page.search_for_links()
add_to.extend(new_links)
return add_to
Õ⽫ÔÚÄ£¿é¼ÓÔصÄʱºòÒÆ×ßʵÀý»¯µÄÄÚÈÝ£¬ÒÔ±ãÿ´ÎÔËÐк¯Êýʱ¶¼»á·¢ÉúÁбíʵÀý»¯¡£Çë×¢Ò⣬¶ÔÓÚ²»¿É±äÊý¾ÝÀàÐÍ£¬±ÈÈçÔª×é¡¢×Ö·û´®¡¢ÕûÐÍ£¬ÊDz»ÐèÒª¿¼ÂÇÕâÖÖÇé¿öµÄ¡£ÕâÒâζ×Å£¬ÏñÏÂÃæÕâÑùµÄ´úÂëÊǷdz£¿ÉÐеģº

def func(message="my message"):
print message
2¡¢ ¿É±äÊý¾ÝÀàÐÍ×÷ΪÀà±äÁ¿
ÕâºÍÉÏÃæÌáµ½µÄ×îºóÒ»¸ö´íÎóºÜÏàÏñ¡£Ë¼¿¼ÒÔÏ´úÂ룺

class URLCatcher(object):
urls = []
def add_url(self, url):
self.urls.append(url)
Õâ¶Î´úÂë¿´ÆðÀ´·Ç³£Õý³£¡£ÎÒÃÇÓÐÒ»¸ö´¢´æ URL µÄ¶ÔÏó¡£µ±ÎÒÃǵ÷Óà add_url ·½·¨Ê±£¬Ëü»áÌí¼ÓÒ»¸ö¸ø¶¨µÄ URL µ½´æ´¢ÖС£¿´ÆðÀ´·Ç³£ÕýÈ·°É£¿ÈÃÎÒÃÇ¿´¿´Êµ¼ÊÊÇÔõÑùµÄ£º

a = URLCatcher()
a.add_url('http://www.google.com')
b = URLCatcher()
b.add_url('http://www.bbc.co.hk')
b.urls£º

['http://www.google.com', 'http://www.bbc.co.uk']
a.urls£º

['http://www.google.com', 'http://www.bbc.co.uk']
µÈµÈ£¬Ôõô»ØÊ£¿£¡ÎÒÃÇÏëµÄ²»ÊÇÕâÑù°¡¡£ÎÒÃÇʵÀý»¯ÁËÁ½¸öµ¥¶ÀµÄ¶ÔÏó a ºÍ b¡£°ÑÒ»¸ö URL ¸øÁË a£¬ÁíÒ»¸ö¸øÁË b¡£ÕâÁ½¸ö¶ÔÏóÔõô»á¶¼ÓÐÕâÁ½¸ö URL ÄØ£¿
ÕâºÍµÚÒ»¸ö´íÀýÊÇͬÑùµÄÎÊÌâ¡£´´½¨ÀඨÒåʱ£¬URL ÁÐ±í½«±»ÊµÀý»¯¡£¸ÃÀàËùÓеÄʵÀýʹÓÃÏàͬµÄÁÐ±í¡£ÔÚÓÐЩʱºòÕâÖÖÇé¿öÊÇÓÐÓõģ¬µ«´ó¶àÊýʱºòÄã²¢²»ÏëÕâÑù×ö¡£ÄãÏ£Íûÿ¸ö¶ÔÏóÓÐÒ»¸öµ¥¶ÀµÄ´¢´æ¡£Îª´Ë£¬ÎÒÃÇÐ޸ĴúÂëΪ£º

class URLCatcher(object):
def __init__(self):
self.urls = []
def add_url(self, url):
self.urls.append(url)
ÏÖÔÚ£¬µ±´´½¨¶ÔÏóʱ£¬URL ÁÐ±í±»ÊµÀý»¯¡£µ±ÎÒÃÇʵÀý»¯Á½¸öµ¥¶ÀµÄ¶ÔÏóʱ£¬ËüÃǽ«·Ö±ðʹÓÃÁ½¸öµ¥¶ÀµÄÁÐ±í¡£

3¡¢ ¿É±äµÄ·ÖÅä´íÎó
Õâ¸öÎÊÌâÀ§ÈÅÁËÎÒÒ»¶Îʱ¼ä¡£ÈÃÎÒÃÇ×ö³öһЩ¸Ä±ä£¬²¢Ê¹ÓÃÁíÒ»ÖֿɱäÊý¾ÝÀàÐÍ - ×ֵ䡣

a = {'1': "one", '2': 'two'}
ÏÖÔÚ£¬¼ÙÉèÎÒÃÇÏë°ÑÕâ¸ö×ÖµäÓÃÔÚ±ðµÄµØ·½£¬ÇÒ±£³ÖËüµÄ³õʼÊý¾ÝÍêÕû¡£

b = a
b['3'] = 'three'
¼òµ¥°É£¿ ÏÖÔÚ£¬ÈÃÎÒÃÇ¿´¿´Ô­À´ÄǸöÎÒÃDz»Ïë¸Ä±äµÄ×Öµä a£º

{'1': "one", '2': 'two', '3': 'three'}
ÍÛµÈһϣ¬ÎÒÃÇÔÙ¿´¿´ b£¿

{'1': "one", '2': 'two', '3': 'three'}
µÈµÈ£¬Ê²Ã´£¿ÓеãÂÒ¡­¡­ÈÃÎÒÃÇ»ØÏëһϣ¬¿´¿´ÆäËü²»¿É±äÀàÐÍÔÚÕâÖÖÇé¿öÏ»ᷢÉúʲô£¬ÀýÈçÒ»¸öÔª×飺

c = (2, 3)
d = c
d = (4, 5)
ÏÖÔÚ c ÊÇ (2, 3)£¬¶ø d ÊÇ (4, 5)¡£
Õâ¸öº¯Êý½á¹ûÈçÎÒÃÇËùÁÏ¡£ÄÇô£¬ÔÚ֮ǰµÄÀý×ÓÖе½µ×·¢ÉúÁËʲô£¿µ±Ê¹ÓÿɱäÀàÐÍʱ£¬ÆäÐÐΪÓеãÏñ C ÓïÑÔµÄÒ»¸öÖ¸Õë¡£ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÁî b = a£¬ÎÒÃÇÕæÕý±í´ïµÄÒâ˼ÊÇ£ºb ³ÉΪ a µÄÒ»¸öÒýÓá£ËüÃǶ¼Ö¸Ïò Python ÄÚ´æÖеÄͬһ¸ö¶ÔÏó¡£ÌýÆðÀ´ÓÐЩÊìϤ£¿ÄÇÊÇÒòΪÕâ¸öÎÊÌâÓëÏÈÇ°µÄÏàËÆ¡£Æäʵ£¬ÕâƪÎÄÕÂÓ¦¸Ã±»³ÆΪ¡¸¿É±äÒý·¢µÄÂé·³¡¹¡£
ÁбíÒ²»á·¢ÉúͬÑùµÄÊÂÂð£¿Êǵġ£ÄÇôÎÒÃÇÈçºÎ½â¾öÄØ£¿Õâ±ØÐë·Ç³£Ð¡ÐÄ¡£Èç¹ûÎÒÃÇÕæµÄÐèÒª¸´ÖÆÒ»¸öÁбí½øÐд¦Àí£¬ÎÒÃÇ¿ÉÒÔÕâÑù×ö£º

b = a[:]
Õ⽫±éÀú²¢¸´ÖÆÁбíÖеÄÿ¸ö¶ÔÏóµÄÒýÓ㬲¢ÇÒ°ÑËü·ÅÔÚÒ»¸öеÄÁбíÖС£µ«ÊÇҪעÒ⣺Èç¹ûÁбíÖеÄÿ¸ö¶ÔÏó¶¼ÊǿɱäµÄ£¬ÎÒÃǽ«ÔٴλñµÃËüÃǵÄÒýÓ㬶ø²»ÊÇÍêÕûµÄ¸±±¾¡£
¼ÙÉèÔÚÒ»ÕÅÖ½ÉÏÁÐÇåµ¥¡£ÔÚÔ­À´µÄÀý×ÓÖÐÏ൱ÓÚ£¬A ijºÍ B ijÕýÔÚ¿´×ÅͬһÕÅÖ½¡£Èç¹ûÓиöÈËÐÞ¸ÄÁËÕâ¸öÇåµ¥£¬Á½¸öÈ˶¼½«¿´µ½ÏàͬµÄ±ä»¯¡£µ±ÎÒÃǸ´ÖÆÒýÓÃʱ£¬Ã¿¸öÈËÏÖÔÚÓÐÁËËûÃÇ×Ô¼ºµÄÇåµ¥¡£µ«ÊÇ£¬ÎÒÃǼÙÉèÕâ¸öÇåµ¥°üÀ¨Ñ°ÕÒʳÎïµÄµØ·½¡£Èç¹û¡°±ùÏ䡱ÊÇÁбíÖеĵÚÒ»¸ö£¬¼´Ê¹Ëü±»¸´ÖÆ£¬Á½¸öÁбíÖеÄÌõÄ¿Ò²¶¼Ö¸Ïòͬһ¸ö±ùÏä¡£ËùÒÔ£¬Èç¹û±ùÏä±» A Ð޸ģ¬³ÔµôÁËÀïÃæµÄ´óµ°¸â£¬B Ò²½«¿´µ½Õâ¸öµ°¸âµÄÏûʧ¡£ÕâÀïûÓмòµ¥µÄ·½·¨½â¾öËü¡£Ö»ÒªÄã¼ÇסËü£¬²¢±àд´úÂëµÄʱºò£¬Ê¹Óò»»áÔì³ÉÕâ¸öÎÊÌâµÄ·½Ê½¡£
×ÖµäÒÔÏàͬµÄ·½Ê½¹¤×÷£¬²¢ÇÒÄã¿ÉÒÔͨ¹ýÒÔÏ·½Ê½´´½¨Ò»¸ö°º¹ó¸±±¾£º

b = a.copy()
ÔÙ´Î˵Ã÷£¬ÕâÖ»»á´´½¨Ò»¸öеÄ×ֵ䣬ָÏòÔ­À´´æÔÚµÄÏàͬµÄÌõÄ¿¡£Òò´Ë£¬Èç¹ûÎÒÃÇÓÐÁ½¸öÏàͬµÄÁÐ±í£¬²¢ÇÒÎÒÃÇÐÞ¸Ä×Öµä a µÄÒ»¸ö¼üÖ¸ÏòµÄ¿É±ä¶ÔÏó£¬ÄÇôÔÚ×Öµä b ÖÐÒ²½«¿´µ½ÕâЩ±ä»¯¡£
¿É±äÊý¾ÝÀàÐ͵ÄÂé·³Ò²ÊÇËüÃÇÇ¿´óµÄµØ·½¡£ÒÔÉ϶¼²»ÊÇʵ¼ÊÖеÄÎÊÌ⣻ËüÃÇÊÇһЩҪעÒâ·ÀÖ¹³öÏÖµÄÎÊÌâ¡£ÔÚµÚÈý¸öÏîÄ¿ÖÐʹÓðº¹ó¸´ÖƲÙ×÷×÷Ϊ½â¾ö·½°¸ÔÚ 99% µÄʱºòÊÇûÓбØÒªµÄ¡£ÄãµÄ³ÌÐò»òÐíÓ¦¸Ã±»¸Ä¸Ä£¬ËùÒÔÔÚµÚÒ»¸öÀý×ÓÖУ¬ÕâЩ¸±±¾ÉõÖÁÊDz»ÐèÒªµÄ¡£
±à³Ì¿ìÀÖ£¡ÔÚÆÀÂÛÖпÉÒÔËæʱÌáÎÊ¡£

×÷Õß¼ò½é£º Pete Savage - Peter ÊÇһλ³äÂú¼¤ÇéµÄ¿ªÔ´°®ºÃÕߣ¬ÔÚ¹ýȥʮÄêÀïÒ»Ö±ÔÚÍƹãºÍʹÓÿªÔ´²úÆ·¡£Ëû´Ó Ubuntu ÉçÇø¿ªÊ¼£¬ÔÚÐí¶à²»Í¬µÄÁìÓò×ÔÔ¸²ÎÓëÒôƵÖÆ×÷ÁìÓòµÄÑо¿¹¤×÷¡£ÔÚÖ°Òµ¾­Àú·½Ã棬ËûÆð³õ×÷Ϊ¹«Ë¾µÄϵͳ¹ÜÀíÔ±£¬´ó²¿·Öʱ¼äÔÚ¹ÜÀíºÍ½¨Á¢Êý¾ÝÖÐÐÄ£¬Ö®ºóÔÚ Red Hat µ£ÈÎ CloudForms ²úÆ·µÄÖ÷Òª²âÊÔ¹¤³Ìʦ¡£
±¾ÎÄתÔØ×Ô£ºhttp://www.linuxprobe.com/python-misunderstanding.html

Ãâ·ÑÌṩ×îÐÂLinux¼¼Êõ½Ì³ÌÊé¼®£¬Îª¿ªÔ´¼¼Êõ°®ºÃÕßŬÁ¦×öµÃ¸ü¶à¸üºÃ£¬¿ªÔ´Õ¾µã£ºhttp://www.linuxprobe.com/
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. Ronny ÓÚ 2017-09-11 10:43:56·¢±í:

    pythonѧϰÎóÇø×ܽᣬ²»´í