python学习记录(九)
0911--https://www.cnblogs.com/fnng/archive/2013/05/08/3066054.html
魔法方法、属性
准备工作
为了确保是新型类,应该把_metaclass = type加入到模块的最开始
- class NewType(Object):
- more_code_here
- class OldType:
- more_code_here
在这两个类中NewType是新类,OldType是属于旧类,如果前面嘉善
构造方法
构造方法与其他方法不一样,当一个对象被创建会立即调用构造方法。创建一个Python的构造方法很简单,只要把init方法,从简单的init方法转换成魔法版本的__init__方法就可以了。
- class FooBar:
- def __init__(self):
- self.somevar = 42
- #运行程序
- >>> f = FooBar()
- >>> f.somevar
- 42
重写一个一般方法
每一个类都可能拥有一个或多个超类(父类),他们从超类那里继承行为方法。
(Python pass是空语句,是为了保持程序结构的完整性。pass 不做任何事情,一般用做占位语句。)
因为B类没有hello方法,B类继承了A类,所以会调用A类的hello方法。
- class A:
- def hello(self):
- print ('hello,I am A.')
- class B(A):
- pass
- #运行程序
- >>> b = B()
- >>> b.hello()
- hello,I am A.
在子类中增加功能的最基本的方式就是增加方法。但是也可以重写一些超类的方法来自定义继承的行为。如下:
- class A:
- def hello(self):
- print ('hello,I am A.')
- class B(A):
- def hello(self):
- print ('hello,I am B')
- #运行程序
- >>> b = B()
- >>> b.hello()
- hello,I am B
特殊的构造方法
重写是继承机制中的一个重要内容,对于构造方法尤其重要。看下面的例子:
- class Bird:
- def __init__(self):
- self.hungry = True
- def eat(self):
- if self.hungry:
- print ('Aaaah...')
- self.hungry = False
- else:
- print ('No,thanks!')
- #运行程序
- >>> b = Bird()
- >>> b.eat()
- Aaaah...
- >>> b.eat()
- No,thanks!
这个类中定义了鸟有吃的能力,当它吃过一次后再吃就不会饿了,通过上面的执行结果可以清晰的看到。
那么用SongBird类来继承Bird类,并且给它添加歌唱的方法:
- class Bird:
- def __init__(self):
- self.hungry = True
- def eat(self):
- if self.hungry:
- print ('Aaaah...')
- self.hungry = False
- else:
- print ('No,thanks!')
- class SongBird(Bird):
- def __init__(self):
- self.sound = 'qiuqiu~~'
- def sing(self):
- print (self.sound)
- #运行程序
- >>> s = SongBird()
- >>> s.sing()
- qiuqiu~~
- >>> s.eat()
- Traceback (most recent call last):
- File "<pyshell#13>", line 1, in <module>
- s.eat()
- File "E:/study/python/demo0911/重写添加方法.py", line 5, in eat
- if self.hungry:
- AttributeError: 'SongBird' object has no attribute 'hungry'
异常很清楚地说明了错误:SongBird没有hungry特性。原因是这样的:在SongBird中,构造方法被重写,但新的构造方法没有任何关于初始化hungry特性的代码。为了达到预期的效果,SongBird的构造方法必须调用其超类Bird的构造方法来确保进行基本的初始化。
两种方法实现:
一、调用未绑定的超类构造方法
- class Bird:
- def __init__(self):
- self.hungry = True
- def eat(self):
- if self.hungry:
- print ('Aaaah...')
- self.hungry = False
- else:
- print ('No,thanks!')
- class SongBird(Bird):
- def __init__(self):
- Bird.__init__(self)
- self.sound = 'qiuqiu~~'
- def sing(self):
- print (self.sound)
- #运行程序
- >>> s = SongBird()
- >>> s.sing()
- qiuqiu~~
- >>> s.eat()
- Aaaah...
- >>> s.eat()
- No,thanks!
在SongBird类中添加了一行代码Bird.__init__(self)。在调用一个实例的方法时,该方法的self参数会被自动绑定到实例上(这称为绑定方法)。但如果直接调用类的方法,那么就没有实例会被绑定。这样就可以自由地提供需要的self参数(这样的方法称为未绑定方法)。
通过将当前的实例作为self参数提供给未绑定方法,SongBird就能够使用其超类构造方法,也就是说属性能被设置。
二、使用super函数
- __metaclass__ = type #表明为新式类
- class Bird:
- def __init__(self):
- self.hungry = True
- def eat(self):
- if self.hungry:
- print ('Aaaah...')
- self.hungry = False
- else:
- print ('No,thanks!')
- class SongBird(Bird):
- def __init__(self):
- super(SongBird,self).__init__()
- self.sound = 'qiuqiu~~'
- def sing(self):
- print (self.sound)
- #运行程序
>>> s = SongBird()- >>> s.sing()
- qiuqiu~~
- >>> s.eat()
- Aaaah...
- >>> s.eat()
- No,thanks!
super函数只能在新式类中使用。当前类和对象可以作为super函数的参数使用,调用函数返回的对象的任何方法都是调用超类的方法,而不是当前类的方法。那就可以在SongBird的构造方法中使用Bird,而直接使用super(SongBird,self)。
属性
访问器是一个简单的方法,它能够使用getHeight、setHeight之类的名字来得到或者重绑定一些特性。如果在访问给定的特性时必须要采取一些行动,那么像这样的封装状态变量就很重要。如下:
- class Rectangle:
- def __init__(self):
- self.width = 0
- self.height = 0
- def setSize(self,size):
- self.width,self.height = size
- def getSize(self):
- return self.width,self.height
- #运行程序
- >>> r = Rectangle()
- >>> r.width = 10
- >>> r.height = 5
- >>> r.getSize()
- (10, 5)
- >>> r.setSize((150,100))
- >>> r.width
- 150
在上面的例子中,getSize和setSize方法有一个名为size的假想特性的访问器方法,size是由width和height构成的元组。
property函数
property函数的使用很简单,如果已经编写了一个像上节的Rectangle 那样的类,那么只要增加一行代码:
- __metaclass__ = type
- class Rectangle:
- def __init__(self):
- self.width = 0
- self.height = 0
- def setSize(self,size):
- self.width,self.height = size
- def getSize(self):
- return self.width,self.height
- size = property(getSize,setSize)
- #运行程序
- >>> r = Rectangle()
- >>> r.width = 10
- >>> r.height = 5
- >>> r.size
- (10, 5)
- >>> r.size = 110,120
- >>> r.width
- 110
在这个新版的Retangle 中,property 函数创建了一个属性,其中访问器函数被用作参数(先取值,然后是赋值),这个属性命为size 。这样一来就不再需要担心是怎么实现的了,可以用同样的方式处理width、height 和size。
python学习记录(九)的更多相关文章
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
- Python学习记录day8
目录 Python学习记录day8 1. 静态方法 2. 类方法 3. 属性方法 4. 类的特殊成员方法 4.1 __doc__表示类的描述信息 4.2 __module__ 和 __class__ ...
- Python学习记录day7
目录 Python学习记录day7 1. 面向过程 VS 面向对象 编程范式 2. 面向对象特性 3. 类的定义.构造函数和公有属性 4. 类的析构函数 5. 类的继承 6. 经典类vs新式类 7. ...
- Python学习记录:括号配对检测问题
Python学习记录:括号配对检测问题 一.问题描述 在练习Python程序题的时候,我遇到了括号配对检测问题. 问题描述:提示用户输入一行字符串,其中可能包括小括号 (),请检查小括号是否配对正确, ...
- Python学习笔记九
Python学习笔记之九 为什么要有操作系统 管理硬件,提供接口. 管理调度进程,并且将多个进程对硬件的竞争变得有序. 操作系统发展史 第一代计算机:真空管和穿孔卡片 没有操作系统,所有的程序设计直接 ...
- 实验楼Python学习记录_挑战字符串操作
自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...
- Spring学习记录(九)---通过工厂方法配置bean
1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. package com.guigu.spring.factory; public class C ...
- 【Python学习之九】模块
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 一.模块的使用和安装模块和C语言中的头文件以及Ja ...
- 我的Python学习记录
Python日期时间处理:time模块.datetime模块 Python提供了两个标准日期时间处理模块:--time.datetime模块. 那么,这两个模块的功能有什么相同和共同之处呢? 一般来说 ...
随机推荐
- 【tf.keras】AdamW: Adam with Weight decay
论文 Decoupled Weight Decay Regularization 中提到,Adam 在使用时,L2 与 weight decay 并不等价,并提出了 AdamW,在神经网络需要正则项时 ...
- 删除centos自带的openjdk
[wj@master hadoop]$ rpm -qa | grep javajava-1.7.0-openjdk-1.7.0.191-2.6.15.5.el7.x86_64python-javapa ...
- 在IIS上发布netcore项目
保证电脑上有.net core sdk或者.net core runtime; 需要安装AspNetCoreModule托管模块:DotNetCore.2.0.5-WindowsHosting.exe ...
- 1、使用 as 而不要用 is
public class ShouldAsNotIs { public void ShouldAs() { object a = new ShouldAsNotIs(); var b = a as S ...
- 使用 git 将代码推送到多个仓库
使用 git 将代码推送到多个仓库 起因 起初,在 GitHub 建了一个仓库,200+ 的 commits .后来(终于在眼泪中明白...误
- cogs 728. [网络流24题] 最小路径覆盖问题 匈牙利算法
728. [网络流24题] 最小路径覆盖问题 ★★★☆ 输入文件:path3.in 输出文件:path3.out 评测插件时间限制:1 s 内存限制:128 MB 算法实现题8-3 最 ...
- Linux删除文件 清除缓存
相信很多测试 经常会经历开发叫你清除缓存这种事. 那我们要怎么清呢? 一.首先,确认你要清除的缓存在哪个目录下,然后切换到该目录下,比如 我现在知道我的的缓存目录是在newerp这个目录下,则如图 二 ...
- <a>标签的href和onclick属性【转】
1链接的onclick 事件被先执行,其次是href属性下的动作(页面跳转,或 javascript 伪链接): 2假设链接中同时存在href 与onclick,如果想让href 属性下的动作不执行, ...
- java实现FTP文件下载
package com.vingsoft.util;/*** @author 作者:dujj* @version 创建时间:2020年1月13日 下午5:53:39*/import java.io.F ...
- C++ 链式继承下的虚函数列表
目录 1.虚函数列表的位置 2.虚函数列表的内容 3.链式继承中虚函数列表的内容 注: 虚函数列表 又称为虚表, vtbl , 指向它的指针称为vptr, vs2019中称为__vfptr 操作系 ...