Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9015
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) |
| Date | 2011-07-07 11:04 +0200 |
| Organization | None |
| References | <38cc43c2-7f4d-4b30-9260-cc439fc7000a@a10g2000vbz.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.735.1310029448.1164.python-list@python.org> (permalink) |
Kannan Varadhan wrote:
> 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)
Functions written in Python are also "descriptors", they have a __get__()
method. The seemingly harmless
whatever = MyClass.ClassDefaultTracer
therefore results in something like
whatever = MyClass.__dict__["ClassDefaultTracer"].__get__(None, MyClass)
internally, and whatever becomes an "unbound method", essentially the
DefaultTracer function wrapped in a check that its first argument is a
MyClass instance.
The simplest workaround is probably to spell out the dictionary access
whatever = MyClass.__dict__["ClassDefaultTracer"]
If you want inheritance to work properly you need something more invoved,
perhaps (untested)
f = self.ClassDefaultTracer
try:
f = f.im_func
except AttributeError:
pass
self.InstanceTracer = f
or you wrap the callable in a descriptor:
>>> def DefaultTracer(*args): print args
...
>>> class D(object):
... def __init__(self, f):
... self.f = f
... def __get__(self, *args):
... return self.f
...
>>> class MyClass(object):
... old = DefaultTracer
... new = D(DefaultTracer)
...
>>> MyClass.old
<unbound method MyClass.DefaultTracer>
>>> MyClass.new
<function DefaultTracer at 0x7f0f1c0c45f0>
>>> m = MyClass()
>>> m.old
<bound method MyClass.DefaultTracer of <__main__.MyClass object at
0x7f0f1cda6fd0>>
>>> m.new
<function DefaultTracer at 0x7f0f1c0c45f0>
Not pretty, but in Python 3 the problem is gone...
> 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) Kannan Varadhan <kvaradhan3@gmail.com> - 2011-07-07 01:08 -0700
Re: TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) Peter Otten <__peter__@web.de> - 2011-07-07 11:04 +0200
Re: TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) Peter Otten <__peter__@web.de> - 2011-07-07 14:50 +0200
Re: TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) Kannan Varadhan <kvaradhan3@gmail.com> - 2011-07-10 19:13 -0700
Re: TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-07 22:36 +1000
Re: TypeError: unbound method DefaultTracer() must be called with MyClass instance as first argument (got str instance instead) Paul Rudin <paul.nospam@rudin.co.uk> - 2011-07-07 13:38 +0100
csiph-web