Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50446
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Callable or not callable, that is the question! |
| Date | 2013-07-11 16:11 +0200 |
| Organization | None |
| References | <n6n2ba-ubg.ln1@satorlaser.homedns.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4586.1373551911.3114.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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