473,763 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.PrintInpu t, 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 9369
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.PrintInpu t, 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.Prin tInput, 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
3972
by: ‘5ÛHH575-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 arguments, got 0". Tested with Python 2.3.4 on OpenBSD and Python 2.4 on Win98; same results. Is this a bug in deepcopy, how I dynamically assign the instance method or something else? (See example code for how I did it.) If you're curious as...
18
4744
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
5
2826
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 class methods. """ Now I'm rather new to programming and unfamiliar to some basic concepts of OOP. However I wrote most of my classes in the new style way and by
42
3454
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: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
9
12849
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 implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The natural (but illegal) notation would be something like class A<QueueClasswhere QueueClass :...
2
13931
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 following error: Traceback (innermost last): File "/full/path/to/file/GenerateData.py", line 104, in ? File "/full/path/to/file/GenerateData.py", line 34, in __init__ TypeError: unbound method must be called with class instance 1st argument
1
3567
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 foo(self): pass Foo.foo() # error: unbound method foo().
3
1844
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
2741
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 = classmethod(method) --
0
9389
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.