想象一下:
class A(object):
class B(object):
def __init__(self):
super(B, self).__init__()
Run Code Online (Sandbox Code Playgroud)
这会产生错误:
NameError: global name B is not defined.
我试过了A.B,但后来说它A没有定义.
更新:
我发现了这个问题.
我有一个这样的课:
class A(object):
class B(object):
def __init__(self):
super(B, self).__init__()
someattribute = B()
Run Code Online (Sandbox Code Playgroud)
在该范围内,A尚未定义.
是否可以从子静态方法调用超静态方法?
我的意思是,以通用的方式,到目前为止,我有以下内容:
public class BaseController extends Controller {
static void init() {
//init stuff
}
}
public class ChildController extends BaseController {
static void init() {
BaseController.loadState();
// more init stuff
}
}
Run Code Online (Sandbox Code Playgroud)
并且它有效,但我想以通用方式执行它,比如调用super.loadState(),这似乎不起作用......
超级方法有什么作用?
public DataFetch(Context context) {
super();
this.ctx = context;
}
Run Code Online (Sandbox Code Playgroud)
此构造函数是否使新创建的对象的上下文成为超类的上下文?不是100%肯定这是如何工作的.那么这个super()方法基本上只是说"让我进入超级模式",用非专业人士的话说出来吗?
我对Python super()以及继承和属性有一个非常奇怪的问题.一,代码:
#!/usr/bin/env python3
import pyglet
import pygame
class Sprite(pyglet.sprite.Sprite):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = self.x, self.y
@property
def x(self):
return super().x
@x.setter
def x(self, value):
super(Sprite, self.__class__).x.fset(self, value)
self.rect.centerx = value
@property
def y(self):
return super().y
@y.setter
def y(self, value):
super(Sprite, self.__class__).y.fset(self, value)
self.rect.centery = value
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,我想要什么(对我来说似乎是Pythonic)
#super(Sprite, self.__class__).x.fset(self, value)
super().x = value
Run Code Online (Sandbox Code Playgroud)
虽然不起作用
super().x
Run Code Online (Sandbox Code Playgroud)
得到的价值很好.在这种情况下,x是超类的属性,同时定义了fset和fget.那为什么不起作用呢?
我使用Python 3并希望使用默认argparse.ArgumentParser设置的自定义类
进行换行formatter_class=argparse.RawDescriptionHelpFormatter.我可以成功地做到这一点,但IntelliJ IDEA 2017.1与Python插件(PyCharm)给出了以下代码的警告:
class CustomParser(argparse.ArgumentParser):
def __init__(self, formatter_class=argparse.RawDescriptionHelpFormatter, **kwargs):
# noinspection PyArgumentList
super().__init__(formatter_class=formatter_class, **kwargs) # warning in this line for the last argument if suppression comment above removed
Run Code Online (Sandbox Code Playgroud)
如果使用IntelliJ抑制命令删除注释,则对kwargs的警告是"预期字典,得到字典",但它正在工作.这是一个误报警告,还是在没有警告的情况下可以做得更好?这个警告背后是否有一个真正的问题,它有助于避免?
附带问题:使用
formatter_class = kwargs.pop('formatter_class', argparse.RawDescriptionHelpFormatter)而不是在签名中明确定义命名参数是否有区别?根据PEP20,签名中更明确的更好,对吧?
我想知道什么时候使用Python 3 super()的味道.
Help on class super in module builtins:
class super(object)
| super() -> same as super(__class__, <first argument>)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我super()只使用了没有参数,它按预期工作(由Java开发人员).
问题:
super(type, obj)和何时super(type, type2)?Mother.__init__(...)吗?我有基类Foo与方法垃圾邮件和类Bar,它覆盖垃圾邮件.我需要在一些就地定义的回调对象的方法中调用基类的垃圾邮件:
public class Foo {
public void spam() {
// ...
}
}
public class Bar extends Foo {
@Override
public void spam() {
objectWhichRequireCallback(new Callback {
@Override
public void onCallback() {
super.spam();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
此代码无效,因为super与Callback相关,而不是Bar类.是否可以从就地定义的对象调用super方法?
class C1
def pr
puts 'C1'
end
end
class C2 < C1
def pr
puts 'C2'
super
puts self.method(:pr).source_location
end
end
c = C2.new
c.pr
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,是否可以获取由super(C1::pr在我们的例子中)执行的代码的位置以及我们C2::pr使用source_location方法获得代码的位置?
这个问题与来自python中的基类的Inherit namedtuple相反,其目的是从namedtuple继承子类,反之亦然.
在正常继承中,这有效:
class Y(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
class Z(Y):
def __init__(self, a, b, c, d):
super(Z, self).__init__(a, b, c)
self.d = d
Run Code Online (Sandbox Code Playgroud)
[OUT]:
>>> Z(1,2,3,4)
<__main__.Z object at 0x10fcad950>
Run Code Online (Sandbox Code Playgroud)
但如果基类是namedtuple:
from collections import namedtuple
X = namedtuple('X', 'a b c')
class Z(X):
def __init__(self, a, b, c, d):
super(Z, self).__init__(a, b, c)
self.d = d
Run Code Online (Sandbox Code Playgroud)
[OUT]:
>>> Z(1,2,3,4)
Traceback (most recent call …Run Code Online (Sandbox Code Playgroud) super ×10
python ×6
inheritance ×3
java ×3
oop ×2
python-3.x ×2
callback ×1
class ×1
constructor ×1
kwargs ×1
namedtuple ×1
overriding ×1
ruby ×1
static ×1