Python面向对象高级

直接调用父类方法

class A:
def __init__(self):
print("hello")
class B(A):
def __init__(self):
A.__init__()

多继承

class C(A,B):
# ...
# 多继承的查找循序是从左往右,注意继承顺序。
# 比如调用某个方法,A中也有B中也有,那么默认就用的A的方法。

slots

class Student:
__slots__ = ('name','age')
s = Student()
s.name = "ar" # 正确
s.score = 95 # 报错,attribute err

slots限定只允许动态加name和age这两个实例属性。在子类中会继承slots,子类自身定义slots的时候两者取并集。

@property

property装饰器用来解决反复调用麻烦的问题,简化了函数调用。

# 直接抄例子了
# 不加装饰器
class Student(object): def get_score(self):
return self._score def set_score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value # 加装饰器
class Student(object): @property
def score(self):
return self._score @score.setter
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value # 直接调用函数就可以检验

super

在pytorch和mxnet中继承的时候都有这个东西,一探究竟。

# 举个例子
class A:
def __init__(self):
print("Enter A")
print("Leave A")
class B(A):
def __init__(self):
print("Enter B")
super(B, self).__init__()
print("Leave B")
class C(A):
def __init__(self):
print("Enter C")
super(C, self).__init__()
print("Leave C")
class D(A):
def __init__(self):
print("Enter D")
super(D, self).__init__()
print("Leave D")
class E(B, C, D):
def __init__(self):
print("Enter E")
super(E, self).__init__()
print("Leave E")
E()
'''
Enter E
Enter B
Enter C
Enter D
Enter A
Leave A
Leave D
Leave C
Leave B
Leave E
'''

首先我们要知道,python是解释型语言,执行到哪里就开始解释然后得到结果,所以我们从程序运行处开始看。

  1. 实例化对象E,调用构造函数
  2. 输出Enter E
  3. 开始调用父类init,调用B的init,输出Enter B,发现要掉B的父类,super特殊就在这,要按照mro顺序来调用,所以走到这里要调B的父类A的init时,发现A之前还有CD没有调用,那么此时要去先调用CD的init
  4. 调用C的init,输出Enter C,同样的,发现A之前还有D
  5. 调用D的init,输出Enter D,A之前没有人了
  6. 调用A的init,输出Enter A,输出leave A
  7. 从D返回,输出leave D
  8. ...

所以super的好处是,当多个类继承自同一类的时候,调用父类函数的时候会有一个mro查找顺序,以防止父类的函数被多次调用。至于mro,可以调用E.MRO()来查看。

[Python]面向对象近期笔记-super的更多相关文章

  1. python面向对象学习笔记(一)

    粘贴一些自学过程中的笔记大纲,源文本在pycharm里面写的,有点乱整理一下,部分内容有待补充,书写不一定100%正确,全当数据备份了. 1.面向对象的特性 #你写代码时什么使用面向对象 #处理比较复 ...

  2. Python面向对象——重写与Super

    1本文的意义 如果给已经存在的类添加新的行为,采用继承方案 如果改变已经存在类的行为,采用重写方案 2图解继承.重写与Super 注:上面代码层层关联.super()可以用到任何方法里进行调用,本文只 ...

  3. Python - 面向对象编程 - 使用 super() 的一些注意事项

    super() 详解 https://www.cnblogs.com/poloyy/p/15223443.html 多继承中使用 super() class A: def test(self): pr ...

  4. Python 面向对象笔记

    Python 面向对象课程笔记 前言 Python 面向对象 正文 基本概念 什么是对象: 万物皆对象 对象是具体物体: 拥有属性 拥有行为 封装零散为整体 OOP(Object Oriented P ...

  5. [Python_4] Python 面向对象(OOP)

    0. 说明 Python 面向对象(OOP) 笔记.迭代磁盘文件.析构函数.内置方法.多重继承.异常处理 参考 Python面向对象 1. 面向对象 # -*-coding:utf-8-*- &quo ...

  6. Python学习笔记【第十一篇】:Python面向对象高级

    isinstance(obj,cls)和issubclass(sub,super) class Person(object): def __init__(self, name, age, sex, n ...

  7. python 面向对象十一 super函数

    python 面向对象十一 super函数   super函数用来解决钻石继承. 一.python的继承以及调用父类成员 父类: class Base(object): def __init__(se ...

  8. Python面向对象04 /封装、多态、鸭子类型、类的约束、super

    Python面向对象04 /封装.多态.鸭子类型.类的约束.super 目录 Python面向对象04 /封装.多态.鸭子类型.类的约束.super 1. 封装 2. 多态 3. 鸭子类型 4. 类的 ...

  9. python 面向对象专题(四):封装、多态、鸭子类型、类的约束、super

    https://www.cnblogs.com/liubing8/p/11321099.html 目录 Python面向对象04 /封装.多态.鸭子类型.类的约束.super 1. 封装 2. 多态 ...

随机推荐

  1. python模块之numpy

    Numpy是一个第三方库,是数组相关的运算 通过pip安装:pip install numpy Anaconda python的一个科学计算发行版本,安装后将不必单独安装numpy,下面的库模块也将不 ...

  2. linux 网卡配置文件详解2018-03-07

    转自:https://www.cnblogs.com/ienino/p/7717092.html 配置文件位置:/etc/sysconfig/network-scripts/ifcfg-eth0 1. ...

  3. 2----scrapy框架之代理and日志级和请求传参

    一.代理 爬虫文件 daili.py class DailiSpider(scrapy.Spider): name = 'daili' #allowed_domains = ['www.xxx.com ...

  4. 【ACM】蛇形填数 - 逻辑怪

    蛇形填数 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 1 ...

  5. pip安装python库

  6. Factorization Machines with libFM 论文阅读

    Factorization Machines with libFM https://pan.baidu.com/s/1aAyhHGNSrZQFDfoz8VsHIQ libFM网站:http://www ...

  7. 04-spring中的aop演示

    1 xml配置 1 导包 2 准备对象 package www.test.service; public interface UserService { public abstract void ad ...

  8. MSSql中profiler工具介绍,

    第一次使用profiler感觉真的是很方便. 他的作用可能有很多,但是我只是简单其中一点,就是查看某一时刻数据库执行了哪些操作,执行了哪些语句,执行语句所花费的时间.都可以很方便的查到,这对于我们优化 ...

  9. Ubuntu18.10安装及优化

    最近机器学习很火,想来学习下,先来搭建一个学习平台. https://www.ubuntu.com 下载最新版本的系统,我这里是 18.10 桌面版. 然后进行分区,我这里在vm只创建了 80G的硬盘 ...

  10. Value cannot be null. Parameter name: source

    下图主要想说.net抛错后的优先级, 错误1是根本原因,排第一位: 错误2里的方法包含了错误1,排第二位: 错误3就是整个Action了. 类似这样的错误,按照这样的顺序来解决bug,相信很受用.