草庐IT

python - __init__ 没有被双重继承类调用

coder 2023-08-26 原文

我有以下代码:

from form_utils import forms as betterforms
from django.db import models

class FilterForm(betterforms.BetterForm):
    def __init__(self, *args, **kwargs):
        super(FilterForm, self).__init__(*args, **kwargs)
        print('filter form __init__')

    ...

class NewEntityForm(FilterForm, FileFormMixin):
    def __init__(self, *args, **kwargs):
        super(NewEntityForm, self).__init__(*args, **kwargs)
        # super(FileFormMixin, self).__init__() <-- really does not matter
        print('newentityform __init__')

FileForMixin 定义如下:

class FileFormMixin(object):
    def __init__(self, *args, **kwargs):
        super(FileFormMixin, self).__init__(*args, **kwargs)
        print('file form mixin __init__')

FileFormMixinhttps://github.com/mbraak/django-file-form 提供, 通过 https://github.com/carljm/django-form-utils 改进.

问题是,FileFormMixin__init__永远不会被调用。我该如何解决?我真的需要他们所有人。现在它只执行 FilterFormNewEntityForm构造函数。

更新

所以,我查看了所有提到的类 __init__的,他们正在调用 super()!

FileFormMixin :

class FileFormMixin(object):
    def __init__(self, *args, **kwargs):
        super(FileFormMixin, self).__init__(*args, **kwargs)

BetterForm :

class BetterBaseForm(object):
    ...
        def __init__(self, *args, **kwargs):
        self._fieldsets = deepcopy(self.base_fieldsets)
        self._row_attrs = deepcopy(self.base_row_attrs)
        self._fieldset_collection = None
        super(BetterBaseForm, self).__init__(*args, **kwargs)

class BetterForm(with_metaclass(BetterFormMetaclass, BetterBaseForm),
                 forms.Form):
    __doc__ = BetterBaseForm.__doc__

更多的是,按照@elwin-arens 的建议打印 class' mro,输出如下:

filter form __init__ NewEntityForm.__mro__ (<class 'myapp.forms.NewEntityForm'>, <class 'myapp.forms.FilterForm'>, <class 'form_utils.forms.BetterForm'>, <class 'django.forms.widgets.NewBase'>, <class 'form_utils.forms.BetterBaseForm'>, <class 'django.forms.forms.Form'>, <class 'django.forms.forms.BaseForm'>, <class 'django_file_form.forms.FileFormMixin'>, <class 'object'>) newsiteform __init__

但是__init__因为 FileFormMixin 只有在我按照@tom-karzes 的建议明确调用它时才会执行

最佳答案

tl;dr

从您发布的 NewEntityForm MRO 中,您可以看到类 BaseFormIn the source of django.forms.forms.BaseForm您还可以看到该类不调用 super(BaseForm, self).__init__(),因此负责断开到 FileFormMixin 的链。

在这种情况下,您可以通过像这样更改 NewEntityForm 的基类的顺序来解决此问题:

class NewEntityForm(FileFormMixin, FilterForm):
    def __init__(self, *args, **kwargs):
        super(NewEntityForm, self).__init__(*args, **kwargs)

解释

您可能在想,类 FilterFormFileFormMixin 作为基类并排存在。

实际上,从多个类继承会创建一个方法解析顺序 (MRO),它是按顺序线性遍历的类列表。此顺序决定了 super 在给定上下文中的含义。

一个简单的例子来演示:

class A(object):
  def __init__(self):
    super(A, self).__init__()
    print('A', A.__mro__)

class B(object):
  def __init__(self):
    super(B, self).__init__()
    print('B', B.__mro__)

class C(B, A):
  def __init__(self):
    super(C, self).__init__()
    print('C', C.__mro__)


c = C() 

super 接受一个类并在方法解析顺序中向右移动一个。所以super(C, self).__init__()调用了B.__init__,这应该是显而易见的,但是super(B, self).__init__() 在此上下文中调用 A.__init__ 而不是 object.__init__,这可能不太明显。

这是因为在上面的代码示例中,C 具有以下方法解析顺序:

C (<class 'C'>, <class 'B'>, <class 'A'>, <class 'object'>)

用图片来说,左边是你所期望的,右边是实际发生的事情:

    Mental model             Actual model
        C                        C
        |                        |
       / \                       B
      B   A                      |
      |   |                      A
       \ /                       |   
      object                   object

所以如果 B 不称它为 super:

class A(object):
  def __init__(self):
    super(A, self).__init__()
    print('A')

class B(object):
  def __init__(self):
    print('B')

class C(B, A):
  def __init__(self):
    super(C, self).__init__()
    print('C')


c = C()

这导致 A 的 __init__() 在调用 super(C, self).__init__() 之后没有被调用。

这就引出了一个问题,NewEntityForm 的 MRO 是什么样子的。您可以像这样打印 mro:

print('NewEntityForm.__mro__', NewEntityForm.__mro__)

这可能会显示一个列表,其中 FileFormMixin 出现在一个没有调用它的 super init 方法的类之后,正如 Daniel Roseman 已经说过的那样。

一个可能的解决方案可能是像这样显式调用您要应用的类的 init:

class NewEntityForm(FilterForm):
    def __init__(self, *args, **kwargs):
        super(NewEntityForm, self).__init__(*args, **kwargs)
        FileFormMixin.__init__(self)
        print('newentityform __init__')

或者您可以翻转 NewEntityForm 的基类。

关于python - __init__ 没有被双重继承类调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37771540/

有关python - __init__ 没有被双重继承类调用的更多相关文章

  1. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  6. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  7. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  8. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  9. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  10. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

随机推荐