Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'example:': 0.03; 'subject:not': 0.03; 'attribute': 0.07; 'detect': 0.07; 'modified': 0.07; '*args,': 0.09; 'bug.': 0.09; 'builtin': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'callable': 0.16; 'callable,': 0.16; 'called.': 0.16; 'descriptor': 0.16; 'eckhardt': 0.16; 'foo()': 0.16; 'foo():': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'situation.': 0.16; 'stumbled': 0.16; 'typeerror:': 0.16; 'unexpected': 0.16; 'wrote:': 0.18; 'slightly': 0.19; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header:X-Complaints- To:1': 0.27; 'feature': 0.29; 'skip:@ 10': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'subject:that': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'but': 0.35; 'method': 0.36; 'clear': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Callable or not callable, that is the question! Date: Thu, 11 Jul 2013 16:11:47 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a98c.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373551911 news.xs4all.nl 15990 [2001:888:2000:d::a6]:60262 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50446 Ulrich Eckhardt wrote: > Hello! > > I just stumbled over a case where Python (2.7 and 3.3 on MS Windows) > fail to detect that an object is a function, using the callable() > builtin function. Investigating, I found out that the object was indeed > not callable, but in a way that was very unexpected to me: > > class X: > @staticmethod > def example(): > pass > test1 = example > test2 = [example,] > > X.example() # OK > X.test1() # OK > X.test2[0]() # TypeError: 'staticmethod' object is not callable Slightly modified example: >>> @staticmethod ... def example(): return 42 ... >>> class X: ... example = example ... >>> X.example() 42 >>> example() Traceback (most recent call last): File "", line 1, in TypeError: 'staticmethod' object is not callable When you access an attribute its __get__() method is implicitly called. This is part of the descriptor protocol: >>> X.example >>> example >>> example.__get__(X) While it would be easy to make staticmethod callable >>> class Staticmethod(staticmethod): ... def __call__(self, *args, **kw): ... return self.__func__(*args, **kw) ... >>> @Staticmethod ... def foo(): return "bar" ... >>> foo() 'bar' >>> X.baz = foo >>> X.baz() 'bar' I see no clear advantage over the current situation. > Bug or feature? No bug. Missing feature if you come up with a convincing use-case.