Head First Python 学习札记 2016-04-10
2016-04-10 20:19
591 查看
Python术语和常识
29将代码打包在类的里面
1Python中类的定义及实例化
2在类中建立新的方法和新的属性
30继承Python的内置list
继承类的方法
23、self—这是一个方法参数,总是指向当前对象实例。类中定义的每个方法,都必须提供self作为第一个参数
以下是利用继承重写的Athlete
29将代码打包在类的里面
1Python中类的定义及实例化
2在类中建立新的方法和新的属性
30继承Python的内置list
继承类的方法
Python术语和常识:
22、不能在两个py文件之间相互引用,下面这种方法就是错误的。#C.py from A import b
#A.py from C import d
23、self—这是一个方法参数,总是指向当前对象实例。类中定义的每个方法,都必须提供self作为第一个参数
29、将代码打包在类的里面
定义一个类的时候实际上是在定义一个定制工厂函数,然后可以在代码中使用这个工厂函数创建实例。1、Python中类的定义及实例化
#创建 class Athlete: '''init前后是有两个下划线的,init就是构造函数,self参数是一个很重要的参数,目标标识符赋至self参数,没有这个赋指,Python解释器无法得出方法调用要应用到哪一个对象实例。self参数可以帮助标识要处理那哟个对象实例的数据,实际上在类中定义的所有方法的第一个参数都是self''' def __init__(self): #The code to initialize a 'Athlete' object. def how_big(self): return (len(self.thing))
#实例化 a = Athlete() #上面这句等效于下面这句 Athlete.__init__(a)
你的代码 | python执行的代码 |
---|---|
d =Athlete(“Holy Grail”) | Athlete.init(d,”Holy”) |
d.how_big() | Athlete.how_big(d) |
解释 | 类名.方法名(目标标识) |
2、在类中建立新的方法和新的属性
class Athlete: #注意根据实际情况设置缺省值 def __init__(self, a_name, a_dob=None, a_times=[]): #通过在构造方法中设置这样的值创造新的属性 self.name = a_name self.dob = a_dob self.times = a_times def top3(self): return (sorted(set([sanitize(t) for t in self.times]))[0:3]) # 记得将self传进去 def add_time(self,time_value): self.times.append(time_value) # 记得将self传进去 def add_times(self,times_value): self.times.extend(times_value)
30、继承Python的内置list
在Python中是支持多重继承的继承类的方法
#在声明子类的参数表里面传进去一个父类 class NamedList(list): def __init__(self,a_name): #初始化所派生的类 list.__init__([]) #把参数赋值给属性 self.name = a_name >>> Johnny = NamedList('John Paul Jones') >>> type(Johnny) <class '__main__.NamedList'> >>> dir(Johnny) #NamedList拥有所用List的方法,还有它特有的属性name来存姓 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'name', 'pop', 'remove', 'reverse', 'sort'] #使用list的方法来补充Johnny的数据,直接把Johnny当成列表就好 >>> Johnny.append("Bass Player") >>> Johnny.extend(['Composetr','Musician']) >>> Johnny ['Bass Player', 'Composetr', 'Musician'] >>> Johnny.name 'John Paul Jones' #由于Johhny是NamedList的实例,而NamedList是list的子类,所以完全可以把Johnny当做列表处理 >>> for attr in Johnny: print(Johnny.name+' is a '+attr+'.') John Paul Jones is a Bass Player. John Paul Jones is a Composetr. John Paul Jones is a Musician.
以下是利用继承重写的Athlete
#利用继承来重写Athlete类 class AthleteList(list): def __init__(self,a_name,a_dob=None,a_times=[]): list.__init__([]) self.name = a_name self.dob = a_dob #因为AthleteList本身就是一个list,就不用再创建times了直接加就好 self.extend(a_times) def top3(self): #这里不遍历self.times而是遍历self return (sorted(set([sanitize(t) for t in self]))[0:3])
相关文章推荐
- python比较文本相似度(not semantical)
- Python
- python中subprocess.Popen.poll
- 【python】matplotlib生成散点矩阵图
- python的逻辑语句
- python+caffe做图片处理可能会用到的方法
- 2016.4.10Python列表生成式及生成器generator
- python全局变量的使用错误
- Sublime Text 3 python 配置
- 华为OJ平台练习题
- python之sys
- Python collections OrderedDict
- python的Image和skimage处理图片
- think python chapter2
- Python os模块
- think python chapter3
- ubuntu中用python把文件的编码改成utf8
- [python]使用txt保存和读取列表变量
- python中的错误处理
- python 装饰器和 functools 模块