Hi. Today, I ran across this, in Python 2.7.6:
class C: ... pass ... issubclass(C,object) False isinstance(C(),object) True <-- ???
The description of isinstance() in Python 2.7 does not reveal this result (to my reading).
From a duck-typing perspective, one would also not guess that an instance of C would be considered an instance of object:
dir(C()) ['__doc__', '__module__'] dir(object()) ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__ ', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
-> What is the motivation for isinstance(C,object) to return True in Python 2.7? Andy Andreas Maier IBM Senior Technical Staff Member, Systems Management Architecture & Design IBM Research & Development Laboratory Boeblingen, Germany maiera@de.ibm.com, +49-7031-16-3654 ________________________________________________________________________ IBM Deutschland Research & Development GmbH Vorsitzende des Aufsichtsrats: Martina Koederitz Geschaeftsfuehrung: Dirk Wittkopp Sitz der Gesellschaft: Boeblingen Registergericht: Amtsgericht Stuttgart, HRB 243294
This is one of the unfortunate effects of the existence of "old-style" classes in Python 2. The old-style class hierarchy is distinct from the new-style class hierarchy, but instances of old-style classes are still objects (since in Python, *everything* is an object). For new code, and whenever you have an opportunity to refactor old code, you should use new-style classes, by inheriting your class from object (or from another class that inherits from object). On Tue, Oct 21, 2014 at 9:43 AM, Andreas Maier <MAIERA@de.ibm.com> wrote:
Hi. Today, I ran across this, in Python 2.7.6:
class C: ... pass ... issubclass(C,object) False isinstance(C(),object) True <-- ???
The description of isinstance() in Python 2.7 does not reveal this result (to my reading).
From a duck-typing perspective, one would also not guess that an instance of C would be considered an instance of object:
dir(C()) ['__doc__', '__module__'] dir(object()) ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__ ', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
-> What is the motivation for isinstance(C,object) to return True in Python 2.7?
Andy
Andreas Maier IBM Senior Technical Staff Member, Systems Management Architecture & Design IBM Research & Development Laboratory Boeblingen, Germany maiera@de.ibm.com, +49-7031-16-3654 ________________________________________________________________________ IBM Deutschland Research & Development GmbH Vorsitzende des Aufsichtsrats: Martina Koederitz Geschaeftsfuehrung: Dirk Wittkopp Sitz der Gesellschaft: Boeblingen Registergericht: Amtsgericht Stuttgart, HRB 243294
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (python.org/~guido)
On Oct 21, 2014, at 10:13 AM, Guido van Rossum wrote:
For new code, and whenever you have an opportunity to refactor old code, you should use new-style classes, by inheriting your class from object (or from another class that inherits from object).
One nice way to do this module-globally is to set: __metaclass__ = type at the top of your file. Then when you're ready to drop Python 2, it's an easy clean up. Cheers, -Barry
Hm. I've never been a fan of that. EIBTI and such... On Tue, Oct 21, 2014 at 10:53 AM, Barry Warsaw <barry@python.org> wrote:
On Oct 21, 2014, at 10:13 AM, Guido van Rossum wrote:
For new code, and whenever you have an opportunity to refactor old code, you should use new-style classes, by inheriting your class from object (or from another class that inherits from object).
One nice way to do this module-globally is to set:
__metaclass__ = type
at the top of your file. Then when you're ready to drop Python 2, it's an easy clean up.
Cheers, -Barry _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (python.org/~guido)
Hi, The problem is a side effect of the fact that old-style classes are implemented on top of new-style meta-classes. Consequently although C is the "class" of C() it is not its "type".
type(C()) <type 'instance'>
type(C()).__mro__ (<type 'instance'>, <type 'object'>)
therefore
issubclass(type(C()), object) True
which implies
isinstance(C(),object) True
Cheers, Mark. On 21/10/14 17:43, Andreas Maier wrote:
Hi. Today, I ran across this, in Python 2.7.6:
class C: ... pass ... issubclass(C,object) False isinstance(C(),object) True <-- ???
The description of isinstance() in Python 2.7 does not reveal this result (to my reading).
From a duck-typing perspective, one would also not guess that an instance of C would be considered an instance of object:
dir(C()) ['__doc__', '__module__'] dir(object()) ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__ ', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
-> What is the motivation for isinstance(C,object) to return True in Python 2.7?
Andy
Andreas Maier IBM Senior Technical Staff Member, Systems Management Architecture & Design IBM Research & Development Laboratory Boeblingen, Germany maiera@de.ibm.com, +49-7031-16-3654 ________________________________________________________________________ IBM Deutschland Research & Development GmbH Vorsitzende des Aufsichtsrats: Martina Koederitz Geschaeftsfuehrung: Dirk Wittkopp Sitz der Gesellschaft: Boeblingen Registergericht: Amtsgericht Stuttgart, HRB 243294
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/mark%40hotpy.org
participants (4)
-
Andreas Maier
-
Barry Warsaw
-
Guido van Rossum
-
Mark Shannon