Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
TypeError: unbound method DefaultTracer() must be called with MyClassinstance as first argument (got
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Kannan Varadhan, post: 4226490"] Hi Folks, Here is something I don't fully understand. 1 def DefaultTracer(fmt, *args): 2 fmt = 'in DefaultTracer ' + fmt 3 print(fmt % args) 4 pass 5 def myTracer(fmt, *args): 6 fmt = 'in myTracer ' + fmt 7 print(fmt % args) 8 9 class MyClass: 10 ClassDefaultTracer = DefaultTracer 11 def __init__(self): 12 self.InstanceTracer = MyClass.ClassDefaultTracer 13 def trace(self, fmt, *args): 14 self.InstanceTracer(fmt, *args) 15 16 17 DefaultTracer("Testing %s", 'at first') 18 myTracer("Testing %s", 'at first') 19 20 MyClass().trace('Using ClassDefaultTracer') 21 22 # override ClassDefaultTracer for all new instances 23 MyClass.ClassDefaultTracer = myTracer 24 MyClass().trace('Using Overridden ClassDefaultTracer') I want ClassDefaultTracer to store a reference to DefaultTracer and be used by instances of MyClass. Why does line20 give me the following error: $ python foo.py in DefaultTracer Testing at first in myTracer Testing at first Traceback (most recent call last): File "foo.py", line 20, in <module> MyClass().trace('Using ClassDefaultTracer') File "foo.py", line 14, in trace self.InstanceTracer(fmt, *args) TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) Alternately, how can I achieve what I want, i.e. a class-wide default used by all instances created off it, but itself be changeable, so that once changed, the changed default would be used by subsequent newer instances of that class. Thanks, Kannan [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
TypeError: unbound method DefaultTracer() must be called with MyClassinstance as first argument (got
Top