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.
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:29 | admin | set | github: 58876 |
| 2012年04月29日 12:23:30 | ezio.melotti | set | nosy:
+ ezio.melotti |
| 2012年04月29日 11:19:04 | thread13 | set | messages: + msg159599 |
| 2012年04月27日 04:13:08 | benjamin.peterson | set | messages: + msg159439 |
| 2012年04月27日 02:58:42 | thread13 | set | messages: + msg159438 |
| 2012年04月26日 06:00:10 | georg.brandl | set | status: open -> closed nosy: + georg.brandl messages: + msg159358 resolution: wont fix |
| 2012年04月26日 04:05:21 | thread13 | set | status: closed -> open resolution: not a bug -> (no value) messages: + msg159354 |
| 2012年04月26日 02:57:45 | benjamin.peterson | set | status: open -> closed nosy: + benjamin.peterson messages: + msg159353 resolution: not a bug |
| 2012年04月26日 02:56:17 | thread13 | set | messages: + msg159352 |
| 2012年04月26日 02:55:10 | thread13 | set | title: isinstance(obj, object) returns True for _old style_ classes -> isinstance(obj, object) returns True for _old style_ class instances |
| 2012年04月26日 02:53:35 | thread13 | create | |