PythonÄ£¿éѧϰ
¡¡¡¡copyÄ£¿éÓÃÓÚ¶ÔÏóµÄ¿½±´²Ù×÷¡£¸ÃÄ£¿é·Ç³£¼òµ¥£¬Ö»ÌṩÁËÁ½¸öÖ÷ÒªµÄ·½·¨£º
copy.copy
Óë
copy.deepcopy
£¬·Ö±ð±íʾdz¸´ÖÆÓëÉÖÆ¡£Ê²Ã´ÊÇdz¸´ÖÆ£¬Ê²Ã´ÊÇÉÖÆ£¬ÍøÉÏÓÐÒ»¿¨³µÒ»¿¨³µµÄ×ÊÁÏ£¬ÕâÀï²»×÷Ïêϸ½éÉÜ¡£¸´ÖƲÙ×÷Ö»¶Ô¸´ºÏ¶ÔÏóÓÐЧ¡£Óüòµ¥µÄÀý×ÓÀ´·Ö±ð½éÉÜÕâÁ½¸ö·½·¨¡£
dz¸´ÖÆÖ»¸´ÖƶÔÏó±¾Éí£¬Ã»Óи´ÖƸöÔÏóËùÒýÓõĶÔÏó¡£
#coding=gbk
import copy
l1 = [1, 2, [3, 4]]
l2 = copy.copy(l1)
print l1
print l2
l2[2][0] = 50
print l1
print l2
#---- ½á¹û ----
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [50, 4]]
[1, 2, [50, 4]]
ͬÑùµÄ´úÂ룬ʹÓÃÉÖÆ£¬½á¹û¾Í²»Ò»Ñù£º
import copy
l1 = [1, 2, [3, 4]]
l2 = copy.deepcopy(l1)
print l1
print l2
l2[2][0] = 50
print l1
print l2
#---- ½á¹û ----
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [50, 4]]
¸Ä±äcopyµÄĬÈÏÐÐΪ
¡¡¡¡ÔÚ¶¨ÒåÀàµÄʱºò£¬Í¨¹ý¶¨Òå__copy__ºÍ__deepcopy__·½·¨£¬¿ÉÒԸıäcopyµÄĬÈÏÐÐΪ¡£ÏÂÃæÊÇÒ»¸ö¼òµ¥µÄÀý×Ó:
class CopyObj(object):
def __repr__(self):
return "CopyObj"
def __copy__(self):
return "Hello"
obj = CopyObj()
obj1 = copy.copy(obj)
print obj
print obj1
#---- ½á¹û ----
CopyObj
Hello
Ïà¹ØÎĵµ£º
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 023
# Tuple(ÔªËØ×é)ÊDz»¿É±äµÄÁбí
tuple1 = (1, 2, 3)
print tuple1
tuple2 = (tuple1, 4, 5, 6) # Ò»¸öÔªËØ×é¿ÉÒÔ×÷ΪÁíÍâÒ»¸öÔªËØ×éµÄÔªËØ
print tuple2 # ²¢ÇÒÄܹ»ÔÚ´æ´¢µÄʱºò±£³ÖÔʼµÄÂß¼¹Øϵ
for ele in tuple2:
print ele
print '\n'
for ele in tuple2[0]:
pr ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 034
class Person:
def __init__(self, name):
self.name = name # ÕâÒ»¾äÖеÚÒ»¸önameÊÇÀàÖаüº¬µÄÓò£¬µÚ¶þ¸önameÊÇ´«½øÀ´µÄ²ÎÊý
# end of def
def sayHello(self):
print 'Hello!'
# end of def
# end of class
p = Person('Ning')
print p.nam ......
µÚ¾Å¹Ø Image
´ÓÒ³ÃæÉϵÄͼƬ¿ÉÒÔ¿´µ½ÓÐÒ»´®µã£¬ÄÇôÊDz»ÊÇ´ú±í¸Ã¹ØÓëͼÏñµãÓйأ¿ ÎÒÃÇ´ÓÒ³ÃæÔ´Âë¿ÉÒÔ¿´µ½£¬ÓÐÁ½¶ÎÊý×ÖÐòÁÐfirstºÍsecond£¬¶øÓÐÒ»¸öÌáʾfirst+second=? ʲôÒâ˼ÄØ£¿ÄѵÀÊÇ˵(first, second)´ú±íÁËͼÏñµãµÄ×ø±ê£¿²»Ïñ£¬Á½¶ÎÐòÁеij¤¶ÈÓкܴó²îÒì¡£ÄÇôËã·û+»¹ÓÐʲôº¬ÒåÄØ£¬Óп ......
ÑéÖ¤ÊÇ·ñÒѾ°²×°ÁËMySQLdb£º
==========================================================
d:\usr\local\Python25>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] onwin32
Type "help", "copyright", "credits" or "license" for ......