python3下的super()
大家都知道super是用来解决python钻石多重继承出现的基类重复调用的问题,这个就不赘述了,不了解的请点击。
但是我发现还有个问题在于不是钻石继承时继承先后顺序的问题,也就是如果mixin与继承的某子类同时作为某类的父类时,其书写顺序对于super可能产生的不同影响:
假设有个情景是是打印租房信息,有一套房子中的一间婴儿房准备出租:
class room:
def __init__(self,area=120,usedfor='sleep'):
self.area = area
self.usedfor = usedfor def display(self):
print("this is my house") class babyroom(room):
def __init__(self,area=40,usedfor="son",wallcolor='green'):
super().__init__(area,usedfor)
self.wallcolr = wallcolor def display(self):
super().display()
print("babyroom area:%s wallcollor:%s"%(self.area,self.wallcolr)) class rent:
def __init__(self,money=1000):
self.rentmoney = money def display(self):
print("for rent at the price of %s"%self.rentmoney) class agent(babyroom,rent):
# class agent(rent,babyroom):
def display(self):
super().display()
print("rent house agent") agent().display() 在理想中我们希望所有类的都能够输出,也就是:
this is my house
babyroom area:40 wallcollor:green
for rent at the price of 1000
rent house agent 但是实际输出并不是这样的,看到上面两种写法:
写法1的输出:
this is my house
babyroom area:40 wallcollor:green
rent house agent
也就是说并没有调用rent类的display方法 写法二的输出:
for rent at the price of 1000
rent house agent
也就是说babyroom以及room类的display方法都没有调用
需要补充的是两种写法中查看agent.__mro__会发现所有类都在,并没有缺失。
所以我认为在此存在传递中断的问题,当两个父类并不是兄弟类时,遇到super()并不会相互传递,二是直接选择走第一个的那条路线!!
这就提醒我们在mixin入的类里尽量不要使用和其他存在继承关系的类相同的方法名,即使都是表达的是打印信息到屏幕的方法
python3下的super()的更多相关文章
- 论python3下“多态”与“继承”中坑
1.背景: 近日切换到python3后,发现python3在多态处理上,有一些比较有意思的情况,特别记载,供大家参考... 以廖老师的python3教程中的animal 和dog的继承一节的代码做例子 ...
- Python3中的super()函数详解
关于Python3中的super()函数 我们都知道,在Python3中子类在继承父类的时候,当子类中的方法与父类中的方法重名时,子类中的方法会覆盖父类中的方法, 那么,如果我们想实现同时调用父类和子 ...
- Python3下map函数的显示问题
map函数是Python里面比较重要的函数,设计灵感来自于函数式编程.Python官方文档中是这样解释map函数的: map(function, iterable, ...) Return an it ...
- python3下调用系统massagebox对话框
#python3下调用系统massagebox对话框#先安装pwin32插件https://github.com/mhammond/pywin32/releases import win32apiim ...
- python3下获取主流浏览器和python的安装路径
#coding=utf-8#python3下获取主流浏览器和python的安装路径#by dengpeiyou date:2018-07-09import winreg,os #取得浏览器的安装路径d ...
- 在python3下使用OpenCV 显示图像
在Python3下用使用OpenCV比在C,C++里开发不止快捷一点点, 原型开发的时候蛮有用. 这里用的OpenCV 加载图片, 用的imshow画图 # -*- coding: utf-8 -*- ...
- python3下django连接mysql数据库
1.安装pymysql pip install pymysql 有一点需要注意,有的系统(比如ubuntu16.04)同时安装了python2和python3,而比较新的django需要在python ...
- python3下安装aiohttp遇到过的那些坑
python3下安装aiohttp遇到过的那些坑 最近需要用到aiohttp这个库,在安装过程中遇到很多坑.google.baidu后,依然没有找到合适的解决方案. 后来通过去python官方的PyP ...
- 在python3下用PIL做图像处理
Python Imaging Library (PIL)是python下的图像处理模块,支持多种格式,并提供强大的图形与图像处理功能. 目前PIL的官方最新版本为1.1.7,支持的版本为python ...
随机推荐
- How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code = 0) ?
How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code ...
- 转:关于PHP性能优化
原文来自于:http://www.cnblogs.com/qq78292959/archive/2012/12/28/2837272.html 1.升级硬件的一般规则:对于 PHP 脚本而言,主要的瓶 ...
- WEB工程数据库相关安装脚本写作
1. 数据库oracle安装 2. 数据库用户创建,表空间创建,表创建 #!/bin/bash current_path=`pwd` create_tablespace=${current_path} ...
- Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?
Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...
- 如何把iOS代码编译为Android应用
新闻 <iPhone 6/6 Plus中国销量曝光:单月销量650万>:据iSuppli Corp.中国研究总监王阳爆料,iPhone 6和iPhone 6 Plus在国内受欢迎的情况大大 ...
- QT正则表达式学习(Windows目录禁止九个字符)
exp 正则表达式30分钟入门教程 http://deerchao.net/tutorials/regex/regex.htm 元字符 .*^\d\b\s,当然还有\,还有中括号[] .是一个元字符, ...
- HDU 4717The Moving Points warmup2 1002题(三分)
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- MFC中对话框的工具栏的使用
1.新建一个MFC项目:在资源视图中新建Toolbar资源: 2.编辑状态栏: 3.在***Dlg.h文件中添加CToolBar类型或其派生类型的一个变量如:(CdlgToolBar myToolBa ...
- AlgorithmsI PA2: Randomized Queues and Deques Subset
本题的bonus是 因此方法是queue的size 达到了K, 就停止增加元素,保证queue.size() 最大时只有k. Java code: import edu.princeton.cs.al ...
- Surprising Strings(map类)
http://poj.org/problem?id=3096 题意容易理解,开始直接暴力,还是用map写下吧,熟练一下: #include<stdio.h> #include<str ...