Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.05; 'instance': 0.05; 'override': 0.07; 'python': 0.08; 'callable': 0.09; 'instance.': 0.09; 'overridden': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'str': 0.09; 'subject:method': 0.09; 'essentially': 0.11; 'error:': 0.12; 'wrote:': 0.15; '*args)': 0.16; '*args):': 0.16; 'changed,': 0.16; 'myclass': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'simplest': 0.16; 'subject:() ': 0.16; 'subject:instance': 0.16; 'unbound': 0.16; 'workaround': 0.16; 'argument': 0.16; 'method.': 0.16; 'subsequent': 0.16; '>>>': 0.16; 'def': 0.16; 'written': 0.17; 'trace': 0.19; 'wrap': 0.19; '(most': 0.21; 'skip:m 30': 0.21; 'dictionary': 0.23; 'traceback': 0.25; 'changed': 0.25; 'testing': 0.25; 'function': 0.26; 'pass': 0.28; 'class.': 0.29; 'problem': 0.29; 'args)': 0.30; 'from:addr:web.de': 0.30; 'seemingly': 0.30; 'typeerror:': 0.30; 'class': 0.31; 'print': 0.32; 'does': 0.32; 'reference': 0.33; 'to:addr:python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'last):': 0.35; 'try:': 0.35; 'probably': 0.35; 'skip:" 10': 0.36; 'file': 0.36; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'created': 0.38; 'something': 0.38; 'perhaps': 0.39; 'header :Mime-Version:1': 0.39; 'i.e.': 0.39; 'except': 0.39; 'subject:with': 0.39; 'skip:s 20': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'skip:d 20': 0.40; 'give': 0.60; 'results': 0.62; 'here': 0.66; 'want,': 0.71; 'spell': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org 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: Thu, 07 Jul 2011 11:04:15 +0200 Organization: None References: <38cc43c2-7f4d-4b30-9260-cc439fc7000a@a10g2000vbz.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bbe3.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 104 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310029448 news.xs4all.nl 21763 [2001:888:2000:d::a6]:51501 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9015 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 > 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 >>> MyClass.new >>> m = MyClass() >>> m.old > >>> m.new 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.