Issue 4326: type of UserList instance returns class instead of instance

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()
type(lu)

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)
True
>>> inspect.isclass(lu)
False
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(a)
<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:
    ...
else:
    ...

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?