如果将对 super 的调用保存在变量中以供将来使用,会发生什么情况?

发布于 2024-12-09 01:30:40 字数 466 浏览 0 评论 0原文

如果我对这里显而易见的事情一无所知,请原谅我,但是如果您将对 super 的调用保存在变量中并稍后使用它会发生什么。

这是类定义的一部分,向您展示我的意思。

class CaselessDict(dict):

    def __init__(self, *args, **kwargs):
        self.super = super(CaselessDict, self) # save super
        self.update(*args, **kwargs)

    def __getitem__(self, key):
        key = self.parsekey(key)
        return self.super.__getitem__(key) # use saved super

当我实现这个 CaselessDict 类时出现了这个问题,几乎每个方法都有 super 。

Forgive me if I'm being ignorant of the obvious here, but what would happen if you save a call to super in a variable and use it later.

Here is a part of a class definition to show you what I mean.

class CaselessDict(dict):

    def __init__(self, *args, **kwargs):
        self.super = super(CaselessDict, self) # save super
        self.update(*args, **kwargs)

    def __getitem__(self, key):
        key = self.parsekey(key)
        return self.super.__getitem__(key) # use saved super

This came up when I was implementing this CaselessDict class and almost every method had super in it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-12-16 01:30:40

预期的事情发生了:self.super 将只保存一个 super 对象,其行为就像 super(CaselessDict, self) 一样。

这种方法的问题在于,每个实例只有一个 super 属性,因此,如果您有更多的类使用此技术,则只有最后分配给 super 的类才会使用该技术。功能正常。哎哟!因此,这样做通常不是一个好主意。您可以将属性命名为__super,这样它就会被破坏,但我建议像其他人一样调用super(CaselessDict, self)

当然,Python 3 中的草更绿,简单的 super() 调用就足够了。

The expected thing happens: self.super will just hold a super object that acts just like super(CaselessDict, self).

The problem with this approach is that each instance only has one super attribute, so if you had more classes that would use this technique, only the one that assigned to super last would function properly. Ouch! So, it's generally not a good idea to do this. You could name the attribute __super so it gets mangled, but I recommend just calling super(CaselessDict, self), as everybody else does.

Of course, the grass is greener in Python 3, where a plain super() call is enough.

很糊涂小朋友 2024-12-16 01:30:40

宇宙将会爆炸。不,应该不会发生任何错误,即使这不是通常的用法。郑重声明,在 Python 3 中,您可以只使用 super().foo(...) ,因此基本上没有什么用处。

The universe will blow up. No, nothing wrong should happen, even though it's not the usual usage. For the record, in Python 3, you can just use super().foo(...), so it's basically useless to do there.

预谋 2024-12-16 01:30:40

我有一个合法的情况,我必须做这种事情,涉及动态加载和重新加载的类。下面是一个片段,加载包含类 A 的模块“a”,并创建它的实例:

>>> import imp
>>> m = imp.find_module("a")
>>> a = imp.load_module("a", *m)
>>> a.A
<class 'a.A'>
>>> aobj = a.A()
>>> aobj.__class__
<class 'a.A'>

现在,如果我重新导入“a”,看看如果我使用之前创建的 aobj 调用 isinstance 会发生什么:

>>> a = imp.load_module("a", *m)
>>> print a.A
<class 'a.A'>
>>> isinstance(aobj, a.A)
False

这与 super 的关系是:从 Python 2.6 开始,super(AClass,self) 添加了检查以确保 self 引用确实是 AClass 的实例。就我而言,我在“A”的方法中使用了 super,但是一旦重新导入“a”模块并重新定义“A”类,我的 super 引用就不再起作用了! aA 类已更改为我现有的 aA 实例不再是其实例,因此 super(aA, self) 将开始引发 TypeErrors。为了解决这个问题,我基本上做了OP所做的事情:在__init__中,我将对 super 的引用保存到实例变量中,然后在其他方法中使用它来向上调用基类方法。

这是我完整的(也许有点漫无目的)博客发布

I had a legitimate case where I had to do this kind of thing, involving classes that were dynamically loaded and reloaded. Here is a snippet that loads a module "a" containing class A, and creates and instance of it:

>>> import imp
>>> m = imp.find_module("a")
>>> a = imp.load_module("a", *m)
>>> a.A
<class 'a.A'>
>>> aobj = a.A()
>>> aobj.__class__
<class 'a.A'>

Now if I reimport "a", see what happens if I call isinstance with the previously created aobj:

>>> a = imp.load_module("a", *m)
>>> print a.A
<class 'a.A'>
>>> isinstance(aobj, a.A)
False

How this relates to super is that as of Python 2.6, super(AClass,self) added the check to make sure that the self reference was indeed an instance of AClass. In my case, I used super in a method of "A", but once the "a" module was reimported and the "A" class redefined, my super references no longer worked! The a.A class had changed to something that my existing instances of a.A were no longer instances of, and so super(a.A, self) would begin to raise TypeErrors. To resolve this, I did essentially what the OP did: in __init__ I save a reference to super into an instance variable, which then gets used in other methods to upcall into base class methods.

Here is my full (and maybe a bit rambling) blog post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文