简体   繁体   English

Python:import.class中的未绑定方法__init __()

[英]Python: Unbound method __init__() from import.class

I'm trying to subclass a class from another python script. 我正在尝试从另一个python脚本中继承一个类。

I've done the following when subclassing from within the same file, and it works. 当我从同一个文件中进行子类化时,我已经完成了以下操作,并且它可以工作。

widge.py
    class widget(object):
        def __init__(self,bob):
            #do something

    class evenWidgetier(widget):
        def __init__(self, bob):
            widget.__init__(self,bob)
            #do something

But once I add in inheritance from another file.. 但是,一旦我从另一个文件中添加继承..

superWidget.py

    import widge
    class superWidgety(widge.evenWidgetier):
        def __init__(self, bob):
            widge.widget.__init__(self,bob)
            #do something

I get an error: 我收到一个错误:

unbound method __init__() must be called with widget instance as first argument

Is there a way I can subclass a class from another package that works? 有没有办法可以从另一个有效的包中继承一个类?

.

And out of curiosity, what's the deal? 出于好奇,这笔交易是什么? Substantively this looks identical to me. 实质上这看起来和我一模一样。 I can call a class from another file by using the widge.widget(), so that method seems established. 我可以使用widge.widget()从另一个文件中调用一个类,这样就可以建立该方法。 And I can subclass when the class is in the same file by referencing the class in the declaration. 通过引用声明中的类,我可以在类在同一个文件中时进行子类化。 What is it about using a class from an import in a declaration that breaks? 在断言的声明中使用导入类是什么意思? Why does it see itself as the right method when in the same file, but sees itself as an unbound method when imported? 为什么它在同一个文件中看作是正确的方法,但在导入时将自己视为未绑定的方法?

The specifically, my code is this (stripping the parts that shouldn't affect this. 具体来说,我的代码是这样的(剥离不应该影响它的部分。

Attributor.py
    class Tracker(object):
        def __init__(self, nodeName=None, dag=None):
            #Tracking stuff

    class Transform(Tracker):
        #Does stuff with inherited class

timeline_tab.py
    import Attributor as attr

    class timeline(attr.Transform):
        #some vars
        def __init__(self, nodeName=None):
            attr.Transform.__init__(self,nodeName=nodeName)
            #Additional init stuff, but doesn't happen because error on previous line

In superWidget.py change the SuperWidget to use super superWidget.py改变SuperWidget使用super

    import widge
    class superWidgety(widge.evenWidgetier):
        def __init__(self, bob):
            super(SuperWidget,self).__init__(bob)
            #do something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM