V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  doraemon1293  ›  全部回复第 1 页 / 共 2 页
回复总数  26
1  2  
@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())
```
2018-12-12 18:18:25 +08:00
回复了 zealinux 创建的主题 Python 求教 Python 合并元组算法
没仔细看题,,,,忽略我写的吧..
2018-12-12 18:17:10 +08:00
回复了 zealinux 创建的主题 Python 求教 Python 合并元组算法
set(itertools.chain(*arr))
2018-07-13 16:57:01 +08:00
回复了 hoxis 创建的主题 Python Python for 循环中的陷阱
一句话总结 ()的出来的 express comprehension 是 generator 不是 tuple
2018-03-22 18:33:15 +08:00
回复了 qxy 创建的主题 程序员 刚打算入门学习算法,遇到一题发现 PHP 果然是最好的语音
随手一写竟然 timecost 排第一。。。。

class Solution:
"""
@param: dictionary: an array of strings
@return: an arraylist of strings
"""
def longestWords(self, dictionary):
# write your code here
ans=[]
longest=0
for word in dictionary:
if len(word)>longest:
ans=[word]
longest=len(word)
elif len(word)==longest:
ans.append(word)
return ans
2018-01-31 18:38:01 +08:00
回复了 suyuanhxx 创建的主题 职场话题 怎样才能获得出国工作的机会?
@binux 老哥现在在英国哪里工作 我也在英国 能给个内推吗
2017-11-17 18:00:59 +08:00
回复了 shanelau 创建的主题 推广 程序员加班炒美股
无脑买腾讯和阿里就行了..
@msg7086 你有病吧 谁惜得管你写什么 是你自己要求这么写的 还写了一堆话来证明自己正确 回头又否定自己
@msg7086 嗯嗯嗯 说得对 以后这种问题你就这么写
@msg7086
@XueSeason
递归啊
dfs bfs 随便
写一堆 for 那要求 7 个时候怎么办 n 个呢?
研究生时老师给的问题 把一个数拆成 6 个数的和 遇到一个哥们 就写了 5 个 for 这家伙还是清华毕业的
1  2  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2420 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 36ms · UTC 16:11 · PVG 00:11 · LAX 09:11 · JFK 12:11
Developed with CodeLauncher
♥ Do have faith in what you're doing.