记录一下这段时间看《Head First Python》记录的一些小知识,只是记了很少一部分,有需要的话以后再添加吧。
for循环的使用:
for 目标标识符 in 列表: 处理代码if语句的使用:
if 满足某个条件: true组 else: false组-
len() BIF会提供某个数据对象的长度,或集合的项数。
-
isinstance() BIF会检查一个标识符是否为某个指定类型。
-
使用可选参数: def print_lol(the_list , level = 0)
下面附一些代码:
#code of open file
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp = data.strip().split(',')
return (AthleteList(temp.pop(0) , temp.pop(0) , temp)
except IOError as e:
print('File Error:' + str(e))
return (None)
#code with great style
def top3(self):
return (sorted(set([sanitize(i) for i in self]))[0:3])
#demo of pickle
import pickle
try:
with open ("data.pickle" , 'wb') as mysavedata:
pickle.dump([1 , 2 , 'three'] , mysavedata)
except pickle.PickleError as e:
print('File Error:' + str(e))
#demo of class
class AthleteList(list):
det __init__(self , a_name , a_dob = None , a_times = []):
self.name = a_name
self.dob = a_dob
self.extend(a_times)
def top3(self):
return (sorted(set([sanitize(i) for i in self]))[0:3])