@@ -289,7 +289,7 @@ def __missing__(self, key):
289
289
dct ["foo" ].append (1 ) # 这有点类似于collections.defalutdict
290
290
dct ["foo" ] # [1]
291
291
292
- #-- 元组和列表的唯一区别在于元组是不可变对象,列表时可变对象
292
+ #-- 元组和列表的唯一区别在于元组是不可变对象,列表是可变对象
293
293
a = [1 , 2 , 3 ] # a[1] = 0, OK
294
294
a = (1 , 2 , 3 ) # a[1] = 0, Error
295
295
a = ([1 , 2 ]) # a[0][1] = 0, OK
@@ -315,7 +315,7 @@ def __missing__(self, key):
315
315
fp .isatty () # 文件是否是一个终端设备文件(unix系统中的)
316
316
fp .tell () # 返回文件操作标记的当前位置,以文件的开头为原点
317
317
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表示以文件末尾为原点进行计算。
319
319
fp .seekable () # 是否可以seek
320
320
fp .truncate ([size ]) # 把文件裁成规定的大小,默认的是裁到当前文件操作标记的位置。
321
321
for line in open ('data' ):
@@ -368,7 +368,8 @@ def __missing__(self, key):
368
368
A = 1 if X else 2
369
369
A = 1 if X else (2 if Y else 3 )
370
370
# 也可以使用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"
372
373
373
374
#-- Python的while语句或者for语句可以带else语句 当然也可以带continue/break/pass语句
374
375
while a > 1 :
@@ -671,7 +672,8 @@ def foo(count=0): # 这里的0是数字, 是不可变的
671
672
672
673
"""IO操作"""
673
674
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.
675
677
raw_input ([prompt ]) # 设置输入,输入都是作为字符串处理
676
678
open (name [, mode [, buffering ]]) # 打开文件,与file有什么不同?推荐使用open
677
679
@@ -710,7 +712,7 @@ def add(x,y):return x + y
710
712
repr (object ) # 将一个对象变幻为可打印的格式
711
713
slice (start , stop [, step ]) # 产生分片对象
712
714
type (object ) # 返回该object的类型
713
- vars ([object ]) # 返回对象的变量名、变量值得字典
715
+ vars ([object ]) # 返回对象的变量名、变量值的字典
714
716
a = Class (); # Class为一个空类
715
717
a .name = 'qi' , a .age = 9
716
718
vars (a ) # {'name':'qi', 'age':9}
@@ -828,7 +830,7 @@ def giveRaise(self, percent, bonus = .10):
828
830
#-- 返回1中 数据属性spam是属于类 而不是对象
829
831
I1 = C1 ('bob' ); I2 = C2 ('tom' ) # 此时I1和I2的spam都为42 但是都是返回的C1的spam属性
830
832
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
832
834
833
835
#-- 类方法调用的两种方式
834
836
instance .method (arg ...)
@@ -891,7 +893,7 @@ def selfless(message)
891
893
x ('hello world' )
892
894
x = Spam .doit # 类的无绑定方法对象 类名 + 函数
893
895
x (obj , 'hello world' )
894
- x = Spam .selfless # 类的无绑定方法是函数 在3.0之前无效
896
+ x = Spam .selfless # 类的无绑定方法函数 在3.0之前无效
895
897
x ('hello world' )
896
898
897
899
#-- 获取对象信息: 属性和方法
@@ -1082,8 +1084,7 @@ def hello(self, name='world'):
1082
1084
# 动态类型语言中 类可以动态创建 type函数可用于创建新类型
1083
1085
def fn (self , name = 'world' ): # 先定义函数
1084
1086
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)
1087
1088
h = Hello () # 此时的h和上边的h一致
1088
1089
1089
1090
0 commit comments