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


Groups > comp.lang.python > #50448

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jason.swails@gmail.com>
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; 'subject:not': 0.03; '(even': 0.05; 'attribute': 0.07; 'detect': 0.07; 'builtin': 0.09; 'function,': 0.09; 'subject:question': 0.10; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; '(#1,': 0.16; 'argument:': 0.16; 'attribute,': 0.16; 'callable': 0.16; 'callable,': 0.16; 'cc:name:python list': 0.16; 'eckhardt': 0.16; 'inst': 0.16; 'nameerror:': 0.16; 'positional': 0.16; 'stumbled': 0.16; 'typeerror:': 0.16; 'unexpected': 0.16; 'x()': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'thu,': 0.19; '>>>': 0.22; 'example': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; '&gt;&gt;&gt;': 0.24; 'cc:2**0': 0.24; '2.0': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'subject:that': 0.31; 'file': 0.32; 'class': 0.32; 'linux': 0.33; '(most': 0.33; 'subject:the': 0.34; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'accessing': 0.36; 'method': 0.36; 'example,': 0.37; 'expected': 0.38; 'skip:& 10': 0.38; '8bit%:4': 0.38; 'jason': 0.38; 'list,': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'skip:x 10': 0.40; 'even': 0.60; 'back': 0.62; 'more': 0.64; 'jul': 0.74; 'behavior': 0.77; '2013,': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=2WRyTxiEhCTNkNEIl2lK8ZRkP9EPdcODvceB1v/Twww=; b=ErGXG/FPq9mp1uXqsxgdz3ave4RP4raUsd7Cm/O2aQBl4bRnqPxaxZdBD/SujYHdmr JI+TPz28xBzTyxoPaCQgJvLCTLV+larK9hbrLSaCFKNg/atJdFs+/vswReg+8shFXsqP YyB76G+Zm0B0poGf1nemAoeZY1//em405BafHdyuCOSy5imZFAaWIfcNTyEnU7bvWt/U V07v14zdan9CyOiwipu1cwis4x1c16JWfpX1QX3QU3ZC69RR63nMGiQVjIkyJs1w5osz MghnW8MI5T+NGp0KI1owq17reySrRBNnjFW1TS64pRGYNjk87BaVTG8iOlnGO6gjKYDY 34Ug==
MIME-Version 1.0
X-Received by 10.42.36.198 with SMTP id v6mr11253636icd.58.1373552914787; Thu, 11 Jul 2013 07:28:34 -0700 (PDT)
In-Reply-To <n6n2ba-ubg.ln1@satorlaser.homedns.org>
References <n6n2ba-ubg.ln1@satorlaser.homedns.org>
Date Thu, 11 Jul 2013 10:28:34 -0400
Subject Re: Callable or not callable, that is the question!
From Jason Swails <jason.swails@gmail.com>
To Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Content-Type multipart/alternative; boundary=001a11c3a2b090fc2104e13d35b8
Cc python list <python-list@python.org>
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.4587.1373552924.3114.python-list@python.org> (permalink)
Lines 192
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373552924 news.xs4all.nl 15878 [2001:888:2000:d::a6]:37772
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50448

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Thu, Jul 11, 2013 at 9:05 AM, Ulrich Eckhardt <
ulrich.eckhardt@dominolaser.com> 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
>

Interestingly, you can actually use this approach to 'fake' staticmethod
before staticmethod was even introduced.  By accessing example from inside
the test2 class attribute list, there is no instance bound to that method
(even if an instance was used to access it).

Using Python 3.3:

Python 3.3.2 (default, Jun  3 2013, 08:29:09)
[GCC 4.5.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...     def example(): pass
...     test = example,
...
>>> X.test[0]()
>>>

Using Python 2.0 (pre-staticmethod):

Python 2.0.1 (#1, Aug 28 2012, 20:25:41)
[GCC 4.5.3] on linux3
Type "copyright", "credits" or "license" for more information.
>>> class X:
... def example(): pass
... test = example,
...
>>> X.test[0]()
>>> staticmethod
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: There is no variable named 'staticmethod'

Once you change test into an instance attribute, you get back to the
expected behavior

Python 3.3.2 (default, Jun  3 2013, 08:29:09)
[GCC 4.5.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...     def example(self): pass
...     test = example,
...
>>> inst = X()
>>> inst.example()
>>> inst.test[0]()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: example() missing 1 required positional argument: 'self'
>>> inst.test = inst.example,
>>> inst.test[0]()
>>>

All the best,
Jason

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