[Head First Python]6. summary

1- 字典-内置数据结构,数据值与键值关联

键-字典中查找部分

值-字典中数据部分

使用dict()工厂函数或者只用{}可以创建一个空字典

>>> list = {}
>>> list['name']='hello'
>>> list['pwd']='world'
>>> list['name']
'hello'
>>> list['Occupation']=['1','2','3']
>>> list['Occupation'][-1]
'3'
>>> list['Occupation'][-2]
'2'

2- 类 用def __init__()方法初始化对象实例 

类中每个方法都必须提供self作为第一个参数

类中每个属性前面都必须有self,从而将数据与其实例关联

类可以从0创建,也可以从python内置类或其他定制类继承

类可以放置到模块中

从0创建

 1 class hi:
 2     def __init__(self, a_name, a_dob=None, a_times=[]):
 3         self.name = a_name
 4         self.dob = a_dob
 5         self.times = a_times
 6 
 7 sarah = hi('sarah', '2002-1-1', ['1','2','0','-1'])
 8 james = hi('james')
 9 
10 print(type(sarah))
11 print(type(james))
12 
13 print(sarah)
14 print(james)
15 
16 print(sarah.name + ' ' +str (sarah.dob) + ' ' +str(sarah.times))
17 print(james.name + ' ' +str (james.dob) + ' ' +str(james.times))

继承内置类

 1 class NameList(list):
 2     def __init__(self,a_name):
 3         list.__init__([])
 4         self.name = a_name
 5 
 6 johnny = NameList("John Paul Jones")
 7 print(type(johnny))
 8 print(dir(johnny))
 9 
10 johnny.append("Bass Player")
11 johnny.extend(["a","b","c"])
12 print(johnny)
13 print(johnny.name)
原文地址:https://www.cnblogs.com/galoishelley/p/3796641.html