English 中文(简体)
如果你在将来使用的一个变量中发出超级呼吁,会发生什么情况?
原标题:What happens if you save a call to super in a variable for future use?
  • 时间:2011-10-09 11:41:31
  •  标签:
  • python
  • 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

这是在我执行这个无案可循的类别时出现的,几乎每一种方法都已经过时。

最佳回答

预期发生的事情是:<代码>本身.super 。 物体,如<代码>超级(无分部分,自封)

这种做法的问题是,每一例只有一个super/code>属性,因此,如果你有更多使用这一技术的班级,那么只有指定到super的班级才能正常运作。 Ouch! 因此,这样做通常不是一种好的想法。 页: 1 姓名-super 因此,我建议只打上<条码>(无分明,自封)<条码>,正如其他人所做的那样。

当然,草场是3台绿化物,在这里,一个平原<条码>超级()的电话足够。

问题回答

宇宙将受到打击。 无论怎样,都不应发生任何错误,即使这并非通常的用法。 关于记录,在Sharma3中,你只能使用super(......)foo(......),因此基本上没有用处。

我有一个合法的例子,即Ihad<>/em>做此类事情,涉及有活力的负荷和重载的班级。 此处是装上包含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”是符合先前提出的反对意见的话,我会看到发生的情况:

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

与超级公司的关系是,正如第2.6条所示,<代码>super(AClass, Self)增加了检查,以确保自我参照确实是AClass的例证。 在我的案件中,我使用的是“A”方法,但一旦“a”模块重新输入并重新定义“A”类,我的超级参考资料不再奏效! a.A类已改为我现有的A.A类不再发生的情况,因此,<代码>超级(a.A.,自封)<代码/代码>将开始提高类型误差。 为了解决这个问题,我基本上做了以下工作:_init__。 我只提及超级公司,将其归入一个案例变量,然后在其他方法中用于召回基类方法。

这里是我的全身(并可能是一种比喻)





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...