PythonÈëÃŵÄ36¸öÀý×Ó Ö® 31
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
# 035
class Person:
population = 0 #Õâ¸ö±äÁ¿ÊÇÊôÓÚÕû¸öÀàµÄ
def __init__(self, name):
self.name = name
print '³õʼ»¯ %s' % self.name
Person.population += 1
# end of def
def __del__(self):
print '%s says bye.' % self.name
Person.population -= 1
if Person.population == 0:
print 'ÎÒÊÇ×îºóÒ»¸öÈËÁË£¡'
else:
print '»¹ÓÐ%d¸öÈË¡£' % Person.population
# end of if
# end of def
def sayHi(self):
print 'Hi, my name is %s' % self.name
# end of def
def howMany(self):
print Person.population
# end of def
# enf of class
ning = Person('Ning')
ning.sayHi()
ning.howMany()
#zhang = Person('Zhang')
#zhang.sayHi()
#zhang.howMany()
#ning.sayHi()
#ning.howMany()
output:
×¢Ò⣡ÏÂÃæÊÇÁ½´ÎÔËÐеĽá¹û£¬¿ÉÒÔ¿´³ödel£¨Îö¹¹º¯Êý£©µÄÔËÐÐʱ¼äÊDz»È·¶¨µÄ¡£Õâ¸öÏÖÏóµÄÔÒòÔÚÓÚPythonµÄÀ¬»ø»ØÊÕ»úÖÆµÄ×Ô¶¯»ØÊÕʱ¼äÊDz»È·¶¨µÄ¡£
³õʼ»¯ Ning
Hi, my name is Ning
1
>>>
³õʼ»¯ Ning
Ning says bye.
ÎÒÊÇ×îºóÒ»¸öÈËÁË£¡
Hi, my name is Ning
0
>>>
Ïà¹ØÎĵµ£º
¡¡¡¡ÓÐʱºò£¬Òª°ÑÄÚ´æÖеÄÒ»¸ö¶ÔÏó³Ö¾Ã»¯±£´æµ½´ÅÅÌÉÏ£¬»òÕßÐòÁл¯³É¶þ½øÖÆÁ÷ͨ¹ýÍøÂç·¢Ë͵½Ô¶³ÌÖ÷»úÉÏ¡£PythonÖÐÓкܶàÄ£¿éÌṩÁËÐòÁл¯Óë·´ÐòÁл¯µÄ¹¦ÄÜ£¬È磺marshal, pickle, cPickleµÈµÈ¡£½ñÌì¾Í½²½²marshalÄ£¿é¡£
¡¡¡¡×¢Ò⣺
marshal²¢²»ÊÇÒ»¸öͨÓõÄÄ£¿é£¬ÔÚijЩʱºòËüÊÇÒ»¸ö²»±»ÍƼöʹÓõÄÄ£¿é£¬ÒòΪʹÓÃmarshalÐ ......
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
# 026
aList = ['1','2','3','4']
aListCopy = aList # Æäʵ£¬ÕâÀï½ö½ö¸´ÖÆÁËÒ»¸öÒýÓÃ
del aList[0]
print aList
print aListCopy # Á½¸öÒýÓÃÖ¸ÏòÁËÁËͬһ¸ö¶ÔÏó£¬ËùÒÔ´òÓ¡½á¹ûÒ»Ñù
aListCopy = aList[:] # ÕâÊǸ´ÖÆÕû¸ö¶ÔÏóµÄÓÐЧ·½·¨
del aList[0]
print aList
print aListCopy
......
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
# 028
consoleInput = raw_input('ÇëÊäÈëµãʲô°É£º')
aFile = file(r'C:\out.txt', 'w')
aFile.write(consoleInput + '\n')
aFile.write('ÕâÀïÊǵڶþÐÐ')
aFile.close()
output£º
>>>
ÇëÊäÈëµãʲô°É£ºhaha
>>>
......
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()
output£º
>>>
This is the first line.
This is the second line.
This is ......
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
e.g.1
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('ÓÖÌí¼ÓÁËÒ»ÐС£')
aFile.close()
output:
e.g.2
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('ÓÖÌí¼ÓÁËÒ»ÐС£')
aFile.close()
output:
e.g.3
ʵÏÖ¸ù¾ÝÔʼÎļþÓÐûÓÐ×îºóÒ»ÐпÕÐеÄÇé¿öÀ´½øÐÐ&ldqu ......