@@ -285,11 +285,11 @@ class Dict(dict):
285285 def  __missing__ (self , key ):
286286 self [key ] =  []
287287 return  self [key ]
288-  dct  =  Dict ()
288+  dct  =  dict ()
289289 dct ["foo" ].append (1 ) # 这有点类似于collections.defalutdict 
290290 dct ["foo" ] # [1] 
291291
292- #-- 元组和列表的唯一区别在于元组是不可变对象,列表时可变对象  
292+ #-- 元组和列表的唯一区别在于元组是不可变对象,列表是可变对象  
293293 a  =  [1 , 2 , 3 ] # a[1] = 0, OK 
294294 a  =  (1 , 2 , 3 ) # a[1] = 0, Error 
295295 a  =  ([1 , 2 ]) # a[0][1] = 0, OK 
@@ -315,7 +315,7 @@ def __missing__(self, key):
315315 fp .isatty () # 文件是否是一个终端设备文件(unix系统中的) 
316316 fp .tell () # 返回文件操作标记的当前位置,以文件的开头为原点 
317317 fp .next () # 返回下一行,并将文件操作标记位移到下一行。把一个file用于for … in file这样的语句时,就是调用next()函数来实现遍历的。 
318-  fp .seek (offset [,whence ]) # 将文件打操作标记移到offset的位置。whence可以为0表示从头开始计算 ,1表示以当前位置为原点计算。2表示以文件末尾为原点进行计算。 
318+  fp .seek (offset [,whence ]) # 将文件打开操作标记移到offset的位置。whence为0表示从头开始计算 ,1表示以当前位置为原点计算。2表示以文件末尾为原点进行计算。 
319319 fp .seekable () # 是否可以seek 
320320 fp .truncate ([size ]) # 把文件裁成规定的大小,默认的是裁到当前文件操作标记的位置。 
321321 for  line  in  open ('data' ): 
@@ -368,7 +368,8 @@ def __missing__(self, key):
368368 A  =  1  if  X  else  2 
369369 A  =  1  if  X  else  (2  if  Y  else  3 )
370370 # 也可以使用and-or语句(一条语句实现多个if-else) 
371-  result  =  (a  >  20  and  "big than 20"  or  a  >  10  and  "big than 10"  or  a  >  5  and  "big than 5" )
371+  a  =  6 
372+  result  =  (a  >  20  and  "big than 20"  or  a  >  10  and  "big than 10"  or  a  >  5  and  "big than 5" ) # 返回"big than 20" 
372373
373374#-- Python的while语句或者for语句可以带else语句 当然也可以带continue/break/pass语句 
374375 while  a  >  1 :
@@ -671,7 +672,8 @@ def foo(count=0): # 这里的0是数字, 是不可变的
671672
672673 """IO操作""" 
673674 file (filename  [, mode  [, bufsize ]]) # file类型的构造函数。 
674-  input ([prompt ]) # 获取用户输入,推荐使用raw_input,因为该函数将不会捕获用户的错误输入 
675+  input ([prompt ]) # 获取用户输入,推荐使用raw_input,因为该函数将不会捕获用户的错误输入,意思是自行判断类型 
676+  # 在 Built-in Functions 里有一句话是这样写的:Consider using the raw_input() function for general input from users. 
675677 raw_input ([prompt ]) # 设置输入,输入都是作为字符串处理 
676678 open (name [, mode [, buffering ]]) # 打开文件,与file有什么不同?推荐使用open 
677679
@@ -710,7 +712,7 @@ def add(x,y):return x + y
710712 repr (object ) # 将一个对象变幻为可打印的格式 
711713 slice (start , stop [, step ]) # 产生分片对象 
712714 type (object ) # 返回该object的类型 
713-  vars ([object ]) # 返回对象的变量名、变量值得字典  
715+  vars ([object ]) # 返回对象的变量名、变量值的字典  
714716 a  =  Class (); # Class为一个空类 
715717 a .name  =  'qi' , a .age  =  9 
716718 vars (a ) # {'name':'qi', 'age':9} 
@@ -828,7 +830,7 @@ def giveRaise(self, percent, bonus = .10):
828830#-- 返回1中 数据属性spam是属于类 而不是对象 
829831 I1  =  C1 ('bob' ); I2  =  C2 ('tom' ) # 此时I1和I2的spam都为42 但是都是返回的C1的spam属性 
830832 C1 .spam  =  24  # 此时I1和I2的spam都为24 
831-  I1 .spam  =  3  # 此时I1新增自有属性spam 值为2  I2和C1的spam还都为24 
833+  I1 .spam  =  3  # 此时I1新增自有属性spam 值为3  I2和C1的spam还都为24 
832834
833835#-- 类方法调用的两种方式 
834836 instance .method (arg ...)
@@ -891,7 +893,7 @@ def selfless(message)
891893 x ('hello world' )
892894 x  =  Spam .doit  # 类的无绑定方法对象 类名 + 函数 
893895 x (obj , 'hello world' )
894-  x  =  Spam .selfless  # 类的无绑定方法是函数  在3.0之前无效 
896+  x  =  Spam .selfless  # 类的无绑定方法函数  在3.0之前无效 
895897 x ('hello world' )
896898
897899#-- 获取对象信息: 属性和方法 
@@ -1082,8 +1084,7 @@ def hello(self, name='world'):
10821084 # 动态类型语言中 类可以动态创建 type函数可用于创建新类型 
10831085 def  fn (self , name = 'world' ): # 先定义函数 
10841086 print ('Hello, %s.'  %  name )
1085-  Hello  =  type ('Hello' , (object ,), dict (hello = fn )) 
1086-  # 创建Hello类 type原型: type(name, bases, dict) 
1087+  Hello  =  type ('Hello' , (object ,), dict (hello = fn )) # 创建Hello类 type原型: type(name, bases, dict) 
10871088 h  =  Hello () # 此时的h和上边的h一致 
10881089
10891090
0 commit comments