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.

Title: type of UserList instance returns class instead of instance
Type: behavior Stage:
Components: None Versions: Python 2.6
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, chafporte, georg.brandl, rhettinger
Priority: normal Keywords:

Created on 2008-11-14 21:36 by chafporte, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (11)
msg75885 - (view) Author: chafporte (chafporte) Date: 2008-11-14 21:36
from UserList import UserList
lu = UserList()

python2.6 prints: <class 'UserList.UserList'>
python2.5 prints: <type 'instance'>
msg75887 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-11-14 21:41
That is because UserList is now a new-style class as a result of it
inheriting from the new abstract base classes.  I believe this is the
way Guido wanted it and do not see any deleterious impacts from the change.

Recommend closing as "won't fix" or "invalid".
msg75888 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-11-14 21:44
Isn't policy to keep old-style classes around for compatibility in 2.x?
Especially one like UserList which is meant to be used as a base class.
msg75891 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-11-14 21:53
It has been the practice to not switch old-style to new-style just for
the hell of it.  However, we do switch as part of large PEP driven
efforts like the ABC backport.
msg75893 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-11-14 22:04
Fair enough.
msg75896 - (view) Author: chafporte (chafporte) Date: 2008-11-14 23:15
but like that there is no way to detect if the object
is a class or an instance. type() says it's a class in both case !
msg75897 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-11-14 23:22
>>> import inspect
>>> from UserList import UserList
>>> lu = UserList()
>>> inspect.isclass(UserList)
>>> inspect.isclass(lu)
msg75898 - (view) Author: chafporte (chafporte) Date: 2008-11-14 23:54
but for a user define class we have:
>class AAA:
>...  pass
>a = AAA()
<type 'instance'>
and you can compare this with types.InstanceType
and it says True

where for the UserList instance the comparison with 
types.InstanceType says False

it is just not homogenous. and it make the comparison with
types.InstanceType unusable !!!

are you sure this is not breaking the API ?
msg75905 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-11-15 08:06
What good is a comparison with InstanceType for? If you want to check
whether it's an instance of a custom class, you'll have to check for
instances of new-style classes anyway. If you want to check for UserList
instances, use isinstance().
msg75978 - (view) Author: chafporte (chafporte) Date: 2008-11-17 19:36
but in python 2.5 you may do this:

if type(lInstance) == types.InstanceType:

and I don't see an easy way to do this with python 2.6
(feel free to propose a solution if you have one)
msg76002 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-11-18 08:00
I repeat, what is this "easy" condition good for?
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48576
2008-11-18 08:00:44georg.brandlsetmessages: + msg76002
2008-11-17 19:36:49chafportesetmessages: + msg75978
2008-11-15 08:06:31georg.brandlsetnosy: + georg.brandl
messages: + msg75905
2008-11-14 23:54:52chafportesetmessages: + msg75898
2008-11-14 23:22:55rhettingersetmessages: + msg75897
2008-11-14 23:15:05chafportesetmessages: + msg75896
2008-11-14 22:04:41benjamin.petersonsetstatus: open -> closed
resolution: wont fix
messages: + msg75893
2008-11-14 21:53:59rhettingersetmessages: + msg75891
2008-11-14 21:44:30benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg75888
2008-11-14 21:41:13rhettingersetnosy: + rhettinger
messages: + msg75887
2008-11-14 21:36:31chafportecreate