doraemon1293 最近的时间轴更新
doraemon1293

doraemon1293

V2EX 第 187686 号会员,加入于 2016-08-18 18:15:48 +08:00
doraemon1293 最近回复了
@gwy15
明白了 一直以为 super 是调用父类 多谢解答
刚看到你的更正 请忽略我上面的回复
为什么 First.__init__里的 super().__init__调用的是 Second.__init__。
First 的 super 不应该调用他的父类 Base 吗?
感谢回答
两种情况 打印 Third.__mro__ 结果都是
(<class '__main__.Third'>, <class '__main__.First'>, <class '__main__.Second'>, <class '__main__.Base'>, <class 'object'>)
多重继承 super 不是从左到右查找吗 为什么会调用 Second.__init__(self)

我把打印的代码改成标注开始和结束
```
class Base():
def __init__(self):
print("Base")

class First(Base):
def __init__(self):
print("first start")
super().__init__()
print("first end")
class Second(Base):
def __init__(self):
print("second start")
# super().__init__()
print("second end")

class Third(First,Second):
def __init__(self):
super().__init__()
print("third")
print(Third.__mro__)
Third()
```
结果如下
first start
second start
second end
first end
third
为什么 first 先被调用 然而 first 里的 super().__init__()没有任何效果呢?
2020-02-25 00:38:16 +08:00
回复了 fanout 创建的主题 程序员 各位有没有兴趣来加拿大 IT 行业工作?
我以为加拿大的工资很高呢 没想到跟英国差不多.
pyautogui
2020-02-20 18:14:59 +08:00
回复了 Windsooon 创建的主题 程序员 数据结构与算法研讨群(附小测验)
7 分 我还以为能全对呢,,,
2020-01-30 22:18:08 +08:00
回复了 jiaju9 创建的主题 奇思妙想 奇点 1,它的诞生必然需要创造者。
建议楼主先看测不准原理
用 unlocker 试试
2018-12-12 18:24:19 +08:00
回复了 zealinux 创建的主题 Python 求教 Python 合并元组算法
union find
```
from collections import defaultdict


class DSU:
def __init__(self):
self.weights = {}
self.parents = {}

def find(self, x):
if x not in self.parents:
self.parents[x] = x
self.weights[x] = 1
return x
else:
path = [x]
while self.parents[path[-1]] != path[-1]:
path.append(self.parents[path[-1]])
root = path[-1]
for node in path:
self.parents[node] = root
return root

def union(self, elements):
roots = [self.find(e) for e in elements]
heaviest_root = max([(self.weights[root], root) for root in roots])[1]
for root in roots:
if root != heaviest_root:
self.weights[heaviest_root] += self.weights[root]
self.parents[root] = heaviest_root


def merger(A):
"""
:type A: List[int]
:rtype: int
"""
dsu = DSU()
for a in A:
dsu.union(a)
d=defaultdict(set)
for k,x in dsu.parents.items():
d[x].add(k)
return list(d.values())
```
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1006 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 19:32 · PVG 03:32 · LAX 12:32 · JFK 15:32
Developed with CodeLauncher
♥ Do have faith in what you're doing.