Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50446

Re: Callable or not callable, that is the question!

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 <python-python-list@m.gmane.org>
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 <n6n2ba-ubg.ln1@satorlaser.homedns.org>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4586.1373551911.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


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 "<stdin>", line 1, in <module>
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
<function example at 0x7fe45c0ea3b0>
>>> example
<staticmethod object at 0x7fe45aafc090>
>>> example.__get__(X)
<function example at 0x7fe45c0ea3b0>

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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Callable or not callable, that is the question! Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-07-11 15:05 +0200
  Re: Callable or not callable, that is the question! Peter Otten <__peter__@web.de> - 2013-07-11 16:11 +0200
    Re: Callable or not callable, that is the question! Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-07-12 08:41 +0200
      Re: Callable or not callable, that is the question! Duncan Booth <duncan.booth@invalid.invalid> - 2013-07-12 07:36 +0000
        Re: Callable or not callable, that is the question! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-12 09:49 +0000
          Re: Callable or not callable, that is the question! Peter Otten <__peter__@web.de> - 2013-07-12 12:36 +0200
  Re: Callable or not callable, that is the question! Jason Swails <jason.swails@gmail.com> - 2013-07-11 10:28 -0400
  Re: Callable or not callable, that is the question! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-12 02:12 +0000
    Re: Callable or not callable, that is the question! Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-12 01:11 -0600

csiph-web