Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > TypeError: unbound method PrintInput() must be called with test instance as first argument (got test instance instead)

Reply
Thread Tools

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

 
 
arotem
Guest
Posts: n/a
 
      10-16-2005
Hi,

I am trying to call an unbound method (PrintInput) with the object
instance as the first argument but getting the following error:
"TypeError: unbound method PrintInput() must be called with test
instance as first argument (got test instance instead)"

Below is the sample code (test) for this purpose (two files).

Any help is greatly appreciated.

Thanks in Advance, Anat


Sample Code:

File 1 - input_file.py:

#!/usr/bin/env python
from test import *

CMD = (test.PrintInput, float(2))

File 2 - test.py:

from input_file import *

class test:
def __init__(self):
_test = 2
def PrintInput(self, input):
print "Input is = %s"%(input)

if __name__== "__main__":
print "Unit testing"
inst = test()
print CMD
cmd = CMD[0]
param = CMD[1:]

cmd(inst,param) # this is the problematic line

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-16-2005
arotem wrote:
> Hi,
>
> I am trying to call an unbound method (PrintInput) with the object
> instance as the first argument but getting the following error:
> "TypeError: unbound method PrintInput() must be called with test
> instance as first argument (got test instance instead)"
>
> Below is the sample code (test) for this purpose (two files).
>
> Any help is greatly appreciated.


The problem is your circular import. When test.py is invoked, it imports
input_file.py, which in turn imports test, creating a class bound to
the name test.test. Then the rest of test is interpreted, creating a
__main__.test. While both are based on the same code, they are two
different instances of classes.

To fix this, put your test-class in a separate module, and import that
from both input_file.py and test.py

Diez
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-16-2005
arotem wrote:

> Hi,
>
> I am trying to call an unbound method (PrintInput) with the object
> instance as the first argument but getting the following error:
> "TypeError: unbound method PrintInput() must be called with test
> instance as first argument (got test instance instead)"
>
> Below is the sample code (test) for this purpose (two files).
>
> Any help is greatly appreciated.
>
> Thanks in Advance, Anat
>
>
> Sample Code:
>
> File 1 - input_file.py:
>
> #!/usr/bin/env python
> from test import *
>
> CMD = (test.PrintInput, float(2))
>
> File 2 - test.py:
>
> from input_file import *
>
> class test:
> def __init__(self):
> _test = 2
> def PrintInput(self, input):
> print "Input is = %s"%(input)
>
> if __name__== "__main__":
> print "Unit testing"
> inst = test()
> print CMD
> cmd = CMD[0]
> param = CMD[1:]
>
> cmd(inst,param) # this is the problematic line


It is best to avoid situations where you import the main script into modules
used by your program. You end up with to copies of (in your example)
test.py. and therefore two functionally identical but distinct test classes
and that gets you the error message. You can verify that with the following
demo:

#file script_is_main.py
import script_is_main
import __main__

print script_is_main is __main__ # False

A sound layout would instead be to move the test class into a separate
module that is used by both the main script and the test module:

# file test.py
class Test:
def __init__(self):
_test = 2
def PrintInput(self, input):
print "Input is = %s"%(input)

# file input_file.py
import test

CMD = (test.Test.PrintInput, float(2))

# file main.py
#!/usr/bin/env python
import input_file
import test

if __name__== "__main__":
print "Unit testing"
inst = test.Test()
CMD = input_file.CMD
print CMD
cmd = CMD[0]
param = CMD[1:]

cmd(inst, param) # this was the problematic line

Peter

 
Reply With Quote
 
arotem
Guest
Posts: n/a
 
      10-17-2005
Thanks... it solves the problem

 
Reply With Quote
 
arotem
Guest
Posts: n/a
 
      10-17-2005
Thanks... it solves the problem

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: unbound method DefaultTracer() must be called with MyClassinstance as first argument (got str instance instead) Kannan Varadhan Python 5 07-11-2011 02:13 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
TypeError: unbound method must be called with class instance 1st argument void.no.spam.com@gmail.com Python 2 12-21-2006 12:50 AM
meta confusion new.instance() argument 1 must be classobj, not type Brad Clements Python 1 06-22-2004 11:13 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments