python 抽象类、抽象方法的实现
2015-11-18 15:52
656 查看
由于python 没有抽象类、接口的概念,所以要实现这种功能得abc.py 这个类库,具体方式如下
如果子类不实现父类的_getBaiduHeaders方法,则抛出TypeError: Can't instantiate abstract class BaiduHeaders with abstract methods 异常
from abc import ABCMeta, abstractmethod #抽象类 class Headers(object): __metaclass__ = ABCMeta def __init__(self): self.headers = '' @abstractmethod def _getBaiduHeaders(self):pass def __str__(self): return str(self.headers) def __repr__(self): return repr(self.headers) #实现类 class BaiduHeaders(Headers): def __init__(self, url, username, password): self.url = url self.headers = self._getBaiduHeaders(username, password) def _getBaiduHeaders(self, username, password): client = GLOBAL_SUDS_CLIENT.Client(self.url) headers = client.factory.create('ns0:AuthHeader') headers.username = username headers.password = password headers.token = _baidu_headers['token'] return headers
如果子类不实现父类的_getBaiduHeaders方法,则抛出TypeError: Can't instantiate abstract class BaiduHeaders with abstract methods 异常
相关文章推荐
- 安装python MySQLdb报错:pymemcompat.h:10:20: 致命错误的解决
- scikit学习心得——Isotonic Regression
- Python编写微信打飞机小游戏(十二)
- Difference between Range and Xrange in Python
- 折腾Ipython
- 第一个Python程序——博客自动访问脚本
- Python基础学习-爬虫小试2
- python插件
- Python学习小记
- 让cherrypy监听系统内所有活动网卡传入的请求
- python 远程操作ubuntu
- Python 创建函数和代码重用
- python数据类型详解
- Python基础练习
- Python pip安装
- python遍历数组的两种方法
- python类型判断
- Python——赋值语句
- python和numpy的版本、安装位置
- python 线程,GIL 和 ctypes