By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,068 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,068 IT Pros & Developers. It's quick & easy.

Unbound method? What's that?

true911m
P: 92
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
Share this Question
Share on Google+
10 Replies


true911m
P: 92
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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

true911m
P: 92
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
Expert 5K+
P: 6,596
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

true911m
P: 92
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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

P: 1
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

Post your reply

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