必须以X实例作为第一个参数调用未绑定的方法(取而代之的是X实例) [英] unbound method must be called with X instance as first argument (got X instance instead)

查看:72
本文介绍了必须以X实例作为第一个参数调用未绑定的方法(取而代之的是X实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 2代码(2.7.9)中收到以下错误消息: TypeError:未绑定方法issueTransitionJIRA()必须以AccessCd实例作为第一个参数来调用(取而代之的是AccessCd实例)

I get the following error message in my Python 2-Code (2.7.9): TypeError: unbound method issueTransitionJIRA() must be called with AccessCd instance as first argument (got AccessCd instance instead)

所以python抱怨第一个参数应该是我实际传递的类实例.当然我缺少了一些东西,但是我还没有得到,正在寻找类似错误消息的其他答案...谁能帮忙/解释我?非常感谢!

So python complains that the 1st argument should be the class instance which I actually passed..?! Certainly I'm missing something, but I did not get it yet, searching other answers on similar error messages... Can anyone help/explain me? Thanks a lot!

这是我的代码结构:

主文件 w32AccessCd.py :

import transitionStatusTracking

class AccessCd:
    def __init__(self):
        # ...
        self.main()

    def main():
        tST = transitionStatusTracking.tStateTrack(self)
        tST.setup()

    # ...

    def issueTransitionJ(self, issuekey, workflowcmd):
        # ...
        return True     


accesscd = AccessCd()

模块文件 transitionStatusTracking.py :

from w32AccessCd import AccessCd

class tStateTrack:

    def __init__(self, ACCESSCD):
        self.ACCESSCD = ACCESSCD

    def setup(self):
        AccessCd.issueTransitionJ(self.ACCESSCD, self.key, "error")

错误:

TypeError: unbound method issueTransitionJ() must be called with AccessCd instance as first argument (got AccessCd instance instead)

推荐答案

Python运行脚本时,文件存储在__main__命名空间中.另一方面,将其作为模块导入会将其放入<name-of-module>命名空间中.

When Python runs a script, the file is stored in the __main__ namespace. Importing it as a module, on the other hand, puts it in the <name-of-module> namespace.

您正在运行w32AccessCd.py作为主要脚本,定义了__main__.AccessCd.

You are running w32AccessCd.py as the main script, defining the __main__.AccessCd.

然后导入transitionStatusTracking,它会转过来并导入w32AccessCd.这将创建该模块的 second 副本,作为w32AccessCd命名空间.您现在拥有w32AccessCd.AccessCd.这是一个单独的类,与其他类不同.

You then import transitionStatusTracking, which turns around and imports the w32AccessCd. This creates a second copy of that module, as the w32AccessCd namespace. You now also have w32AccessCd.AccessCd. This is a separate class, and is distinct from the other class.

接下来会发生什么:

  • accesscd = AccessCd()创建类型为__main__.AccessCd的实例.
  • __main__.AccessCd.__init__调用__main__.AccessCd.main.
  • __main__.AccessCd.main创建transitionStatusTracking.tStateTrack的实例,并传入__main__.AccessCd实例.
  • 您调用transitionStatusTracking.tStateTrack.setup方法,该方法尝试将w32AccessCd.AccessCd.issueTransitionJ()方法与__main__.AccessCd实例一起使用,但调用失败.
  • accesscd = AccessCd() creates an instance of type __main__.AccessCd.
  • __main__.AccessCd.__init__ calls __main__.AccessCd.main.
  • __main__.AccessCd.main creates an instance of transitionStatusTracking.tStateTrack, passing in the __main__.AccessCd instance in.
  • You call the transitionStatusTracking.tStateTrack.setup method, which tries to use the w32AccessCd.AccessCd.issueTransitionJ() method with a __main__.AccessCd instance, and the call fails.

您可以通过以下三种方法来解决此问题:

You have three options to fix this:

  • transitionStatusTracking模块中,从__main__导入:

from __main__ import AccessCd

并使循环导入有效,您必须将import transitionStatusTracking行移动到下方的 块中,否则该类尚未定义.或者,在代码中使用import __main__并引用__main__.AccessCd.

and to make the circular import work, you'd have to move the import transitionStatusTracking line to below the class AccessCd block, otherwise the class is not defined yet. Alternatively use import __main__ and reference __main__.AccessCd in your code.

现在您拥有相同的课程.

Now you have the same class.

创建第三个文件以执行脚本;说出main.py,并附上:

Create a third file to do the scripting; say main.py, with:

from w32AccessCd import AccessCd
accesscd = AccessCd()

,然后从w32AccessCd模块中删除accesscd = AcessCd()行.现在将只有w32AccessCd.AccessCd类实例被传递.

and remove the accesscd = AcessCd() line from the w32AccessCd module. Now there will be just the w32AccessCd.AccessCd class instance being passed around.

直接调用方法 而不是取消绑定:

Call the method directly rather than unbound:

def setup(self):
    self.ACCESSCD.issueTransitionJ(self.key, "error")

这可以确保方法绑定到正确的实例,并且不再依赖于类的来源.

This ensures the method is bound to the right instance and it no longer matters where the class came from.

这篇关于必须以X实例作为第一个参数调用未绑定的方法(取而代之的是X实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆