所以我有以下重载方法:
private static void foo(short... a)
{
System.out.println("Calling var-len");
}
private static void foo(int a, int b)
{
System.out.println("Calling int-int");
}
private static void foo(int a, double b) //(3)
{
System.out.println("Calling int-double");
}
private static void main (String[] args)
{
foo((short)2, (short)5); //This one outputs "Calling int-int"
}
Run Code Online (Sandbox Code Playgroud)
我知道变量arity方法在方法解析阶段具有最低优先级,所以在这种情况下如果我调用foo((short)2, (short)4);我会得到"Calling int-int".
但是,如果我将方法(3)更改为foo(short a, double b),则选择变量arity方法!(Java 7).有人能解释一下吗?
在Python中的大多数类型/类中,我可以.mro()不带参数调用.但不是type和它的后代:
In [32]: type(4).mro()
Out[32]: [int, object]
In [33]: type(type(4)).mro()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-48a6f7fcd2fe> in <module>()
----> 1 type(type(4)).mro()
TypeError: descriptor 'mro' of 'type' object needs an argument
Run Code Online (Sandbox Code Playgroud)
看来我可以得到我想要的东西type(type(4)).mro(type(4)),但为什么我不能mro()像其他地方那样直接打电话?
我的库中有这样的方法
public void Foo<T>(IQueryable<T> input)
{
//T is never used and the code compiles when I remove T
}
Run Code Online (Sandbox Code Playgroud)
我想重构它并删除泛型参数.
public void Foo(IQueryable input) { ... }
Run Code Online (Sandbox Code Playgroud)
它如何影响依赖于我的库的代码?他们需要重建吗?
他们遇到编译错误吗?
如果他们使用反射调用此方法怎么办?
如果我创建它们,那么方法解析将始终选择通用的方法.如何在通用版本中使用旧版本并在以后的版本中弃用?
我有这段代码,其中使用了超级继承和多重继承。班级成绩是:
go A go!
go C go!
go B go!
go D go!
Run Code Online (Sandbox Code Playgroud)
虽然我期望:
go A go!
go B go!
go D go!
Run Code Online (Sandbox Code Playgroud)
根据我的理解:D因为MRO调用了B类,因为go是在B中实现的。B类调用了它的父A的super。A被执行了,那就可以了。然后我期望 B 继续执行,所以这意味着 B 被执行,最后 D 被执行。但这当然是不正确的。为什么会进入C,因为go方法的定义是在B中找到的,那么它不应该再在C中搜索。这就是MRO的工作原理。它在头等舱中找到,不应该再搜索。完全困惑:(
class A(object):
def go(self):
print("go A go!")
class B(A):
def go(self):
super(B, self).go()
print("go B go!")
class C(A):
def go(self):
super(C, self).go()
print("go C go!")
class D(B,C):
def go(self):
super(D, self).go()
print("go D go!")
a = A()
b = B()
c = C()
d = D()
d.go()
Run Code Online (Sandbox Code Playgroud) Python3.11引入了StrEnumand ,分别IntEnum继承了stror int,并且也继承了ReprEnum,后者又继承了Enum。
ReprEnum的实现实际上是空的。
>>> print(inspect.getsource(ReprEnum))
class ReprEnum(Enum):
"""
Only changes the repr(), leaving str() and format() to the mixed-in type.
"""
Run Code Online (Sandbox Code Playgroud)
如果我创建StrEnum并检查 MRO,我可以看到它str是第一位的。
class Strings(StrEnum):
A = "a"
Run Code Online (Sandbox Code Playgroud)
>>> Strings.__mro__
(<enum 'Strings'>, <enum 'StrEnum'>, <class 'str'>, <enum 'ReprEnum'>, <enum 'Enum'>, <class 'object'>)
Run Code Online (Sandbox Code Playgroud)
和 都str定义Enuma__str__和 a__repr__
>>> str.__repr__
<slot wrapper '__repr__' of 'str' objects>
>>> str.__str__
<slot …Run Code Online (Sandbox Code Playgroud) 我一直试图理解super()多重继承的上下文中的行为.我很困惑为什么super()在test2.py的父类中调用会导致__init__()为父母双方调用?
test1.py
#!/usr/bin/env python
class A(object):
def __init__(self):
self.A = "A"
print self.A
class B(object):
def __init__(self):
self.B = "B"
print self.B
class C(A, B):
def __init__(self):
self.C = "C"
print self.C
super(C, self).__init__()
if __name__ == '__main__':
print "Without super() in parent __init__():"
c = C()
print c.__dict__
print C.__mro__
Run Code Online (Sandbox Code Playgroud)
生产:
$ ./test.py
Without super() in parent __init__():
C
A
{'A': 'A', 'C': 'C'}
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type 'object'>)
Run Code Online (Sandbox Code Playgroud)
test2.py …
python multiple-inheritance super superclass method-resolution-order
鉴于:
In [37]: class A:
....: f = 1
....:
In [38]: class B(A):
....: pass
....:
In [39]: getattr(B, 'f')
Out[39]: 1
Run Code Online (Sandbox Code Playgroud)
好吧,要么叫超级还是爬行mro?
In [40]: getattr(A, 'f')
Out[40]: 1
Run Code Online (Sandbox Code Playgroud)
这是预料之中的.
In [41]: object.__getattribute__(A, 'f')
Out[41]: 1
In [42]: object.__getattribute__(B, 'f')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-42-de76df798d1d> in <module>()
----> 1 object.__getattribute__(B, 'f')
AttributeError: 'type' object has no attribute 'f'
Run Code Online (Sandbox Code Playgroud)
什么是getattribute没有做那个getattr呢?
In [43]: type.__getattribute__(B, 'f')
Out[43]: 1
Run Code Online (Sandbox Code Playgroud)
什么?! type.__getattribute__打电话超级,但object版本不?
In [44]: type.__getattribute__(A, 'f')
Out[44]: 1
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用新的 python 数据类来创建一些混合类(在我写这篇文章时,我认为这听起来像是一个轻率的想法),但我遇到了一些问题。看下面的例子:
从数据类导入数据类
@dataclass
class NamedObj:
name: str
def __post_init__(self):
print("NamedObj __post_init__")
self.name = "Name: " + self.name
@dataclass
class NumberedObj:
number: int = 0
def __post_init__(self):
print("NumberedObj __post_init__")
self.number += 1
@dataclass
class NamedAndNumbered(NumberedObj, NamedObj):
def __post_init__(self):
super().__post_init__()
print("NamedAndNumbered __post_init__")
Run Code Online (Sandbox Code Playgroud)
如果我再尝试:
nandn = NamedAndNumbered('n_and_n')
print(nandn.name)
print(nandn.number)
Run Code Online (Sandbox Code Playgroud)
我得到
NumberedObj __post_init__
NamedAndNumbered __post_init__
n_and_n
1
Run Code Online (Sandbox Code Playgroud)
暗示它已经运行__post_init__了NamedObj,但没有运行NumberedObj。我想要的是让 NamedAndNumbered__post_init__为它的两个混合类 Named 和 Numbered 运行。有人可能认为如果NamedAndNumbered有这样的就可以做到__post_init__:
def __post_init__(self):
super(NamedObj, self).__post_init__()
super(NumberedObj, self).__post_init__()
print("NamedAndNumbered …Run Code Online (Sandbox Code Playgroud) python multiple-inheritance mixins method-resolution-order python-dataclasses
有人可以解释给定代码的输出以及在这种情况下 python MRO 是如何工作的吗?
class A(object):
def go(self):
print("go A go!")
class B(A):
def go(self):
super(B, self).go()
print("go B go!")
class C(A):
def go(self):
super(C, self).go()
print("go C go!")
class D(C, B):
def go(self):
super(D, self).go()
print("go D go!")
d = D()
d.go()
Run Code Online (Sandbox Code Playgroud)
输出:
go A go!
go B go!
go C go!
go D go!
Run Code Online (Sandbox Code Playgroud)
按照从左到右和深度,我会说它应该是:
go A go!
go C go!
go D go!
Run Code Online (Sandbox Code Playgroud)
但似乎它不像我想的那样工作。
python oop inheritance multiple-inheritance method-resolution-order
我目前正在学习 Python 考试,但我还不了解 Python 3 中的 MRO 和线性化。
class F: pass
class G: pass
class H: pass
class E(G,H): pass
class D(E,F): pass
class C(E,G): pass
class B(C,H): pass
class A(D,B,E): pass
Run Code Online (Sandbox Code Playgroud)
例如,在一项作业中,存在一个问题:在 A 类的线性化中,E 是否可能出现在 C 之前。
如何判断是否可以呢?如何尽可能简单地描述线性化算法(C3)?我真的很感谢有关这方面的各种解释和资源,因为我很难理解 Python 中的线性化。
预先非常感谢!
python ×8
inheritance ×3
python-3.x ×2
super ×2
c# ×1
compilation ×1
enums ×1
generics ×1
getattr ×1
java ×1
metaclass ×1
mixins ×1
oop ×1
overloading ×1
superclass ×1