This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: isinstance(obj, object) returns True for _old style_ class instances
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ezio.melotti, georg.brandl, thread13
Priority: normal Keywords:

Created on 2012-04-26 02:53 by thread13, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg159351 - (view) Author: Q (thread13) Date: 2012-04-26 02:53
$python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
>>> class Old: pass
>>> class New(object): pass
>>> o = Old()
>>> n = New()
>>> isinstance(o, object)
True

This is it, basically. Is it a bug or a feature?

More tests :

>>> isinstance(o, Old)
True
>>> isinstance(o, New)
False
>>> isinstance(n, Old)
False
>>> isinstance(o, int)
False

Please note that some unimportant output was deleted from above.

PS. If this is a feature, how do I detect an old-style class then ?
msg159352 - (view) Author: Q (thread13) Date: 2012-04-26 02:56
In addition:

>>> issubclass(Old, object)
False
msg159353 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-04-26 02:57
Yes, everything is a object. issubclass, though, works differently for old-style and new-style classes.
msg159354 - (view) Author: Q (thread13) Date: 2012-04-26 04:05
>>> help(isinstance)

isinstance(...)
    isinstance(object, class-or-type-or-tuple) -> bool
    
    Return whether an object is an instance of a class or of a subclass thereof.
    (...)

So are the old-style class instances descendants of the object?

I feel like I am missing something (except for the fact that you have closed the bug).
msg159358 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-04-26 06:00
This is a result of how old-style classes are implemented.

If you look at type(Old()), you can see that it isn't Old, but "instance".

(And "instance" is a subclass of object again.)

"issubclass" for old-style classes doesn't check type(o) but o.__class__, which are different: the former is "instance" and the latter your class.  That is one reason we removed old-style classes in Python 3...
msg159438 - (view) Author: Q (thread13) Date: 2012-04-27 02:58
I do not mean to reopen the bug (there are supposedly much more important things to work on in Python). 

But just for the record, let me state that I feel like there is some misleading inconsistency here:

- by definition, a new style class is "Any class which inherits from object" ( see http://docs.python.org/glossary.html#term-new-style-class ) ;

- to support this statement, new classes are indeed explicitly defined in the form "NewClass(object)" ;

- now isinstance(), that is supposed to "return whether an object is an instance of a class or of a subclass thereof" (see help(isinstance)), returns True for old-style objects.

It also seems reasonable if the descendants of a class will inherit its powers, which -- in the case of the old-style classes -- they obviously don't.

Furthermore, I personally see no /point/ in returning True for isinstance(Old(), object): as it is quite misleading, one could easily have made it returning e.g. None as well.

As I completely accept the fact it's a feature -- ( may be slightly confusing, and probably also useless -- but ... hey, nobody's perfect ) -- should I take then calling

issubclass(obj.__class__, object) 

to be the official way to distinguish between the new-style and the old-style classes?
msg159439 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-04-27 04:13
2012/4/26 Q <report@bugs.python.org>:
> issubclass(obj.__class__, object)
>
> to be the official way to distinguish between the new-style and the old-style classes?

Just do type(cls) is types.ClassType.
msg159599 - (view) Author: Q (thread13) Date: 2012-04-29 11:19
thanks, that's rather convenient
History
Date User Action Args
2022-04-11 14:57:29adminsetgithub: 58876
2012-04-29 12:23:30ezio.melottisetnosy: + ezio.melotti
2012-04-29 11:19:04thread13setmessages: + msg159599
2012-04-27 04:13:08benjamin.petersonsetmessages: + msg159439
2012-04-27 02:58:42thread13setmessages: + msg159438
2012-04-26 06:00:10georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg159358

resolution: wont fix
2012-04-26 04:05:21thread13setstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg159354
2012-04-26 02:57:45benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg159353

resolution: not a bug
2012-04-26 02:56:17thread13setmessages: + msg159352
2012-04-26 02:55:10thread13settitle: isinstance(obj, object) returns True for _old style_ classes -> isinstance(obj, object) returns True for _old style_ class instances
2012-04-26 02:53:35thread13create