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!
10 46302
OK, I'm missing something really basic here.
Here's what I'm playing with: - class Printer():
-
"""
-
Wraps setup and execution of wx.lib.printout for
-
simple implementation in calling apps.
-
"""
-
-
writeline=[]
-
writelist=[]
-
writedoc={'list':writelist, 'line':writeline}
-
-
-
def Write(self,newtext,doc=writedoc):
-
pass
-
-
if __name__=='__main__':
-
a=Printer
-
print a.writedoc
-
print dir(a)
-
a.Write('ABC')
-
-
When it gets to a.Write(), I receive: - TypeError: unbound method Write()
-
must be called with Printer instance
-
as first argument (got str instance instead)
-
I have tried various replacements to attempt to satisfy that error, such as: - Printer.Write(a,'ABC')
-
-
Write(a,'ABC')
-
-
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....
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()
>>>
- class Printer:
-
"""
-
Wraps setup and execution of wx.lib.printout for
-
simple implementation in calling apps.
-
"""
-
-
writeline=[]
-
writelist=[]
-
writedoc={'list':writelist, 'line':writeline}
-
-
-
def Write(self,newtext,doc=writedoc):
-
pass
-
-
if __name__=='__main__':
-
a=Printer()
-
print a.writedoc
-
print dir(a)
-
a.Write('ABC')
-
-
Watch the parentheses!
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):
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
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).
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.
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
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.
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: -
if __name__ == "__main__":
-
instance = class
-
print instance.test()
-
and this is the possible fix: -
if __name__ == "__main__":
-
instance = class()
-
print instance.test()
-
just my 5 cents. Maybe it will work or maybe not :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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()....
|
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...
|
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)
|
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"...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
| |