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)
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!
Here is my code structure:
main-file 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()
module-file 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")
Error:
TypeError: unbound method issueTransitionJ() must be called with AccessCd instance as first argument (got AccessCd instance instead)
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.You are running
w32AccessCd.py
as the main script, defining the__main__.AccessCd
.You then import
transitionStatusTracking
, which turns around and imports thew32AccessCd
. This creates a second copy of that module, as thew32AccessCd
namespace. You now also havew32AccessCd.AccessCd
. This is a separate class, and is distinct from the other class.What happens next is this:
accesscd = AccessCd()
creates an instance of type__main__.AccessCd
.__main__.AccessCd.__init__
calls__main__.AccessCd.main
.__main__.AccessCd.main
creates an instance oftransitionStatusTracking.tStateTrack
, passing in the__main__.AccessCd
instance in.transitionStatusTracking.tStateTrack.setup
method, which tries to use thew32AccessCd.AccessCd.issueTransitionJ()
method with a__main__.AccessCd
instance, and the call fails.You have three options to fix this:
In the
transitionStatusTracking
module, import from__main__
:and to make the circular import work, you'd have to move the
import transitionStatusTracking
line to below theclass AccessCd
block, otherwise the class is not defined yet. Alternatively useimport __main__
and reference__main__.AccessCd
in your code.Now you have the same class.
Create a third file to do the scripting; say
main.py
, with:and remove the
accesscd = AcessCd()
line from thew32AccessCd
module. Now there will be just thew32AccessCd.AccessCd
class instance being passed around.Call the method directly rather than unbound:
This ensures the method is bound to the right instance and it no longer matters where the class came from.