标签: super

在嵌套类中使用super()

想象一下:

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尚未定义.

python super

23
推荐指数
2
解决办法
5672
查看次数

从静态方法调用超级方法

是否可以从子静态方法调用超静态方法?

我的意思是,以通用的方式,到目前为止,我有以下内容:

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(),这似乎不起作用......

java inheritance static super

23
推荐指数
2
解决办法
3万
查看次数

super()方法有什么作用?

超级方法有什么作用?

public DataFetch(Context context) {
    super();
    this.ctx = context;
}
Run Code Online (Sandbox Code Playgroud)

此构造函数是否使新创建的对象的上下文成为超类的上下文?不是100%肯定这是如何工作的.那么这个super()方法基本上只是说"让我进入超级模式",用非专业人士的话说出来吗?

java constructor class super

21
推荐指数
2
解决办法
3万
查看次数

Python超级和设置父类属性

我对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 super

21
推荐指数
1
解决办法
4985
查看次数

IntelliJ Python 3检查"预期字典,得到一个字典"对于**kwargs超级误报?

我使用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 intellij-idea super kwargs python-3.x

21
推荐指数
1
解决办法
4232
查看次数

在Python 3中调用super()的4种方法中的哪一种?

我想知道什么时候使用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__(...)吗?

python oop super python-3.x

20
推荐指数
2
解决办法
8109
查看次数

Java:如何从内部内部类调用super方法

我有基类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)

此代码无效,因为superCallback相关,而不是Bar类.是否可以从就地定义的对象调用super方法?

java overriding callback super

20
推荐指数
1
解决办法
5430
查看次数

如何找到super执行的代码的source_location?

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方法获得代码的位置?

ruby inheritance super

20
推荐指数
4
解决办法
2839
查看次数

如何在python中使用带有一个参数的super()

在阅读Python中的super()对象时,我阅读了以下语句:

如果省略第二个参数,则返回的超级对象是未绑定的

这究竟是什么意思,我如何super()在代码中使用一个参数?

python super

20
推荐指数
1
解决办法
975
查看次数

继承自namedtuple基类 - Python

这个问题与来自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)

python oop inheritance super namedtuple

20
推荐指数
3
解决办法
1万
查看次数