[New-bugs-announce] [issue7006] The replacement suggested for callable(x) in py3k is not equivalent

Milko Krachounov report at bugs.python.org
Sun Sep 27 16:23:55 CEST 2009


New submission from Milko Krachounov <python at milko.3mhz.net>:

hasattr(x, '__call__') has been suggested as a replacement for
callable(x) in the documentation and in the warning when running
python2.6 with -3. It is also what 2to3 replaces it with. However, the
two are not equivalent.

1. I can add a __call__ attribute to my object with "obj.__call__ =
lambda x:x". That will not make the object callable, callable() on
Python 2.6 returns False, but hasattr(obj, '__call__') returns True.
2. I can implement a __getattr__ that returns something for every
possible attribute name. Again, this doesn't make the object callable,
nor does callable(obj) in Python 2.6 return True.

I think a closer replacement for "callable(x)" would be "'__call__' in
vars(type(x))".

----------
assignee: georg.brandl
components: 2to3 (2.x to 3.0 conversion tool), Documentation, Interpreter Core
messages: 93171
nosy: georg.brandl, milko.krachounov
severity: normal
status: open
title: The replacement suggested for callable(x) in py3k is not equivalent
type: behavior
versions: Python 2.6, Python 3.0, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7006>
_______________________________________


More information about the New-bugs-announce mailing list