doraemon1293's recent timeline updates
doraemon1293

doraemon1293

V2EX member #187686, joined on 2016-08-18 18:15:48 +08:00
doraemon1293's recent replies
@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__()没有任何效果呢?
Feb 25, 2020
Replied to a topic by fanout 程序员 各位有没有兴趣来加拿大 IT 行业工作?
我以为加拿大的工资很高呢 没想到跟英国差不多.
pyautogui
Feb 20, 2020
Replied to a topic by Windsooon 程序员 数据结构与算法研讨群(附小测验)
7 分 我还以为能全对呢,,,
Jan 30, 2020
Replied to a topic by jiaju9 奇思妙想 奇点 1,它的诞生必然需要创造者。
建议楼主先看测不准原理
用 unlocker 试试
Dec 12, 2018
Replied to a topic by 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())
```
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5418 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 21ms · UTC 07:49 · PVG 15:49 · LAX 00:49 · JFK 03:49
♥ Do have faith in what you're doing.