473,396 Members | 2,037 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,396 software developers and data experts.

Unbound method? What's that?

true911m
Can someone give me a quick rundown on the difference between bound and unbound methods? I ran into an error regarding an unbound method, and found lots of discussions about changing Python behavior regarding them, but haven't come across these descriptions in any of my reading material so far.

Thanks!
Dec 4 '06 #1
10 46302
OK, I'm missing something really basic here.

Here's what I'm playing with:


Expand|Select|Wrap|Line Numbers
  1. class Printer():
  2.     """
  3.     Wraps setup and execution of wx.lib.printout for
  4.     simple implementation in calling apps.
  5.     """
  6.  
  7.     writeline=[]
  8.     writelist=[]
  9.     writedoc={'list':writelist, 'line':writeline}
  10.  
  11.  
  12.     def Write(self,newtext,doc=writedoc):
  13.         pass
  14.  
  15. if __name__=='__main__':
  16.     a=Printer
  17.     print a.writedoc
  18.     print dir(a)
  19.     a.Write('ABC')
  20.  
  21.  
When it gets to a.Write(), I receive:

Expand|Select|Wrap|Line Numbers
  1. TypeError: unbound method Write() 
  2. must be called with Printer instance 
  3. as first argument (got str instance instead)
  4.  
I have tried various replacements to attempt to satisfy that error, such as:

Expand|Select|Wrap|Line Numbers
  1. Printer.Write(a,'ABC')
  2.  
  3.     Write(a,'ABC')
  4.  
  5.     a.Write(a,'ABC')
and pretty much anything I can throw at it.

I have looked at several examples of creating a class with a method, and do not know where I am going astray.

Thanks again....
Dec 4 '06 #2
bartonc
6,596 Expert 4TB
Can someone give me a quick rundown on the difference between bound and unbound methods? I ran into an error regarding an unbound method, and found lots of discussions about changing Python behavior regarding them, but haven't come across these descriptions in any of my reading material so far.

Thanks!
The only thing that I've ever needed to remember is that I need to work with instances. For example:


>>> class aClass:
... def aClassFuntion(self):
... pass
...
>>> aClass.aClassFuntion()
File "<console>", line 1, in ?
''' exceptions.TypeError : unbound method aClassFuntion() must be called with aClass instance as first argument (got nothing instead) '''
>>> instance = aClass()
>>> instance.aClassFuntion()
>>>
Dec 4 '06 #3
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. class Printer:
  2.     """
  3.     Wraps setup and execution of wx.lib.printout for
  4.     simple implementation in calling apps.
  5.     """
  6.  
  7.     writeline=[]
  8.     writelist=[]
  9.     writedoc={'list':writelist, 'line':writeline}
  10.  
  11.  
  12.     def Write(self,newtext,doc=writedoc):
  13.         pass
  14.  
  15. if __name__=='__main__':
  16.     a=Printer()
  17.     print a.writedoc
  18.     print dir(a)
  19.     a.Write('ABC')
  20.  
  21.  
Watch the parentheses!
Dec 4 '06 #4
bartonc
6,596 Expert 4TB
When defining a class, use parens to subclass. ie:
class myFrame(wx.Frame):

This is what they call "old style" (but I still use it occationally):
class oldStyleClass:
I haven't missed any "new style" features yet.

New style always subclasses "object" if there is no other superclass:
class newStyleClass(object):
Dec 4 '06 #5
Holy Frijoles -- it's always the 5 cent parts that getcha.

Say, Barton (is it Chris?), are you the only newbie helper that does weekends? :)

Thank you!

- Mark
Dec 4 '06 #6
bartonc
6,596 Expert 4TB
Holy Frijoles -- it's always the 5 cent parts that getcha.

Say, Barton (is it Chris?), are you the only newbie helper that does weekends? :)

Thank you!

- Mark
Not Chris. There about three other experts who check in every now and then.
I am the only one using wx. As moderator, I'm almost alway monitoring (except 4 am to 9 am PST).
Dec 4 '06 #7
I'm moving ahead with an elementary class that lets me set up my framework. I'm cutting some corners, but I'll improve it later as I form better questions to ask. :)

You can close this if you like.
Dec 4 '06 #8
bartonc
6,596 Expert 4TB
I'm moving ahead with an elementary class that lets me set up my framework. I'm cutting some corners, but I'll improve it later as I form better questions to ask. :)

You can close this if you like.
Awesome! Keep posting,
Barton
Dec 4 '06 #9
bartonc
6,596 Expert 4TB
When defining a class, use parens to subclass. ie:
class myFrame(wx.Frame):

This is what they call "old style" (but I still use it occationally):
class oldStyleClass:
I haven't missed any "new style" features yet.

New style always subclasses "object" if there is no other superclass:
class newStyleClass(object):
I'm actually using new-style now.
Jan 28 '07 #10
I've managed to fix this issue with the following, it just happened though:

I could recreate the problem/TypeError by removing the () behind the class instantiating:

Expand|Select|Wrap|Line Numbers
  1. if __name__ == "__main__":
  2.     instance = class
  3.     print instance.test()
  4.  
and this is the possible fix:

Expand|Select|Wrap|Line Numbers
  1. if __name__ == "__main__":
  2.     instance = class()
  3.     print instance.test()
  4.  
just my 5 cents. Maybe it will work or maybe not :)
Aug 16 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ben | last post by:
Hi, I was recently writing a python interpreter (in python), partly to teach myself more about the python object system, and partly to learn more about how programming languages work in general....
4
by: Rick Thiel | last post by:
Hello, I am trying to build a Crystal report VisualStudio.NET for an ASP.NET application. I would like to build a report that doesn't connect to any particular database at design time because I...
4
by: arotem | last post by:
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...
18
by: TORQUE | last post by:
Hi, Im wondering if anyone can help me with a problem. I have a form with more than 50 unbound fields. Some of the fields will be blank from time to time. This seems to be where im having...
2
by: brent.chambers | last post by:
I wrote a small class today at work playing with sockets in command line windows. When attempting to call the handle function, I get a TypeError. "Unbound method handle() must be called with...
2
by: Kevin Walzer | last post by:
I am trying to structure a Tkinter application with classes instead of just with simple functions, but I'm not sure how to call methods from my main class. My main class is packetstreamApp()....
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...
14
by: 7stud | last post by:
Here is some example code that produces an error: class Test(object): def greet(): print "Hello" t = Test() t.greet() TypeError: greet() takes no arguments (1 given)
9
by: prakashwadhwani | last post by:
I have an unbound combo box in the form header. I have used an input mask "CCCC" 40 times to limit the max number of characters to 40. When I tab into the combo box & press a character say "K"...
6
by: Thomas Heller | last post by:
I'm currently using code like this to create unbound methods from functions and stick them into classes: method = new.instancemethod(raw_func, None, cls) setattr(cls, name, method) Ok, python...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.