如果将对 super 的调用保存在变量中以供将来使用,会发生什么情况?
如果我对这里显而易见的事情一无所知,请原谅我,但是如果您将对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
预期的事情发生了:
self.super
将只保存一个super
对象,其行为就像super(CaselessDict, self)
一样。这种方法的问题在于,每个实例只有一个
super
属性,因此,如果您有更多的类使用此技术,则只有最后分配给super
的类才会使用该技术。功能正常。哎哟!因此,这样做通常不是一个好主意。您可以将属性命名为__super
,这样它就会被破坏,但我建议像其他人一样调用super(CaselessDict, self)
。当然,Python 3 中的草更绿,简单的
super()
调用就足够了。The expected thing happens:
self.super
will just hold asuper
object that acts just likesuper(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 tosuper
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 callingsuper(CaselessDict, self)
, as everybody else does.Of course, the grass is greener in Python 3, where a plain
super()
call is enough.宇宙将会爆炸。不,应该不会发生任何错误,即使这不是通常的用法。郑重声明,在 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.我有一个合法的情况,我必须做这种事情,涉及动态加载和重新加载的类。下面是一个片段,加载包含类 A 的模块“a”,并创建它的实例:
现在,如果我重新导入“a”,看看如果我使用之前创建的 aobj 调用 isinstance 会发生什么:
这与 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:
Now if I reimport "a", see what happens if I call isinstance with the previously created aobj:
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 sosuper(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.