1、背景:

近日切换到python3后,发现python3在多态处理上,有一些比较有意思的情况,特别记载,供大家参考。。。

以廖老师的python3教程中的animal 和dog的继承一节的代码做例子,上代码先:

#!/usr/bin/env python3
# -*- coding: utf-8 -*- class Animal(object):
def run1(self):
print('Animal is running 1...')
def run2(self):
self.run1()
print('Animal is running 2...') class Cat(Animal):
def run1(self,name):
print('[%s] Cat is running1...' %name) def run2(self,name=""):
super().run2()
print('[%s] cat is running2...' %name) def run_twice(animal):
animal.run1("")
animal.run2("") if __name__=='__main__':
c = Cat()
run_twice(c)

输出结果:

[1] Cat is running1...

报错信息如下:

 File "d:\python\tensf\clstest.py", line 28, in <module>
run_twice(c)
 File "d:\python\tensf\clstest.py", line 23, in run_twice
animal.run2("")
 File "d:\python\tensf\clstest.py", line 17, in run2
super().run2()
 File "d:\python\tensf\clstest.py", line 8, in run2
self.run1() builtins.TypeError: run1() missing 1 required positional argument: 'name'

2、分析原因:

1、父类animal中run2()调用了run1()

2、子类cat中覆盖了run1(),增加了name参数,并覆盖了run2(),同样增加了name参数,并调用父类animal中run2()

3、理想中的状态,父类的run2()应该是调用父类的run1(),实际却是调用子类的run1(),所以导致参数匹配错误。

builtins.TypeError: run1() missing 1 required positional argument: 'name'

解决方案要分情况,就本例而言给name赋上默认值即可。

3、延伸

问题来源于自己写了configparser的扩展包,实现给get(),getint(),set()加默认值的方法,在python2中好用,移到python3中突然不好用了,有点发懵。

不过仔细分析,还是python3中configparser的get()有修改。

困扰了我接近一天,还是基本功有问题,贴上我写的简单代码。

补充一点:python3下默认有configparser,无需额外用pip安装,而且大写改成了小写。

#coding=utf-8
'''
Date :2016.10.8
Author : joshua zou Purpose :
configparser 的扩展类,增加默认值,兼容key不存在的情况。
Use exap:
import eConfig as eTax
INICONFIG=eTax.eConfig()
#读取配置文件中配置
debuglevel = INICONFIG.get('default','debuglevel')
'''
try:
from configparser import OrderedDict as _default_dict
except ImportError:
# fallback for setup.py which hasn't yet built _collections
_default_dict = dict from configparser import RawConfigParser class eConfig(RawConfigParser ):
def __init__(self, defaults=None, dict_type=_default_dict,
allow_no_value=False):
super().__init__(defaults, dict_type,allow_no_value) def get(self, section, option, default='',**kwargs):
try :
sRet = super().get(section, option,**kwargs)
except:
sRet = default
return sRet def getint(self, section, option,default=None,**kwargs):
try :
sRet = super().getint(section, option,**kwargs)
except Exception as e :
sRet = default
return sRet def getfloat(self, section, option,default=None,**kwargs):
try :
sRet = super().getfloat(section, option)
except:
sRet = default
return sRet def getboolean(self, section, option,default=None,**kwargs):
try :
sRet = super().getboolean(section, option)
except:
sRet = default
return sRet def set(self, section, option,value):
if not super().has_section(section):
sRet = super().add_section(section)
sRet = super().set(section, option, value)
return sRet if __name__ == "__main__":
#读取配置
filename = r'zhbook.ini'
sf=eConfig()
sf.read(filename) print (sf.get('name', 'lastchp',''))
print (sf.getint('name', 'lastchp',0))
print (sf.get('default', 'taskcount1', ''))
print (sf.get('default', 'taskcount1'))
print (sf.getint('default', 'taskcount1'))
print (sf.getboolean('default', 'taskcount1'))
print (sf.getfloat('default', 'taskcount1'))
print (sf.set('default2', 'taskcount1',u'')) #保存配置
fp = open(filename,"w")
sf.write(fp)
fp.close()
print (sf.get('default', 'taskcount1'))
sf.remove_option('default','taskcount1')
fp = open(filename,"w")
sf.write(fp)
fp.close()

最新文章

  1. mfc+vtk
  2. SQL Server如何编辑超过前200行的数据
  3. tkinter 在 x window 下的字体设置格式
  4. 工具fiddler学习
  5. DNS正向解析与反向解析
  6. HD1814Peaceful Commission(模板题)
  7. JavaScript 获取当前时间戳
  8. error-iis-Service Unavailable
  9. 使用Amoeba 实现MySQL DB 读写分离
  10. 2.5.1 使用alertDialog
  11. php基础知识【函数】(9)数学和对象类函数
  12. Angularjs快速入门(一)
  13. gulp + gulp-better-rollup + rollup 构建 ES6 开发环境
  14. SpringBoot笔记--FastJson
  15. Linux 常用命令——df, du, ln
  16. Atitit 数据库视图与表的wrap与层级查询规范
  17. Xcode密钥没有备份或者证书过期,出现Valid Signing错误
  18. USACO Section1.1
  19. 【问底】王帅:深入PHP内核(一)——弱类型变量原理探究
  20. MVC4实现AJAX需要引用的2个文件

热门文章

  1. CSS实现文字换行
  2. 0基础手把手教你搭建webpack运行打包项目(未完待续)
  3. Struts配置详解
  4. JDBC结果集rs.next()注意事项
  5. 一些常用的vim编辑器快捷键:
  6. php中static 静态关键字
  7. ArcGIS 网络分析[8.6] 资料6 创建网络分析图层及进行路径分析
  8. UVALive 3716 DNA Regions
  9. 【原创】java NIO FileChannel 学习笔记 新建一个FileChannel
  10. JavaScript的DOM编程--06--两个实验