473,221 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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

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

Oct 16 '05 #1
4 9321
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
Oct 16 '05 #2
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

Oct 16 '05 #3
Thanks... it solves the problem :-)

Oct 17 '05 #4
Thanks... it solves the problem :-)

Oct 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: 5HH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
5
by: Martin P. Hellwig | last post by:
While I was reading PEP 8 I came across this part: """ Function and method arguments Always use 'self' for the first argument to instance methods. Always use 'cls' for the first argument to...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
2
by: void.no.spam.com | last post by:
I'm a novice at Python, and found some code samples on how to use threads. My script is being run by a product that contains a Jython interpreter. Can someone please explain why I get the...
1
by: cinsky | last post by:
Hi, I got confused when I learned the function datetime.today(). So far I learned, unless an instance is created, it is not possible to call the class method. For example: class Foo: def...
3
by: Reckoner | last post by:
would it be possible to use one of an object's methods without initializing the object? In other words, if I have: class Test: def __init__(self): print 'init' def foo(self): print 'foo'
13
by: Hussein B | last post by:
Hi, I'm familiar with static method concept, but what is the class method? how it does differ from static method? when to use it? -- class M: def method(cls, x): pass method =...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.