Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if object is instance of new-style user-defined class?

Code:

import types


class C(object):
    pass


c = C()
print(isinstance(c, types.InstanceType))

Output:

False

What correct way to check if object is instance of user-defined class for new-style classes?

UPD:

I want put additional emphasize on if checking if type of object is user-defined. According to docs:

types.InstanceType
The type of instances of user-defined classes.

UPD2:

Alright - not "correct" ways are OK too.

UPD3:

Also noticed that there is no type for set in module types

like image 936
Gill Bates Avatar asked Jan 30 '13 20:01

Gill Bates


3 Answers

You can combine the x.__class__ check with the presence (or not) of either '__dict__' in dir(x) or hasattr(x, '__slots__'), as a hacky way to distinguish between both new/old-style class and user/builtin object.

Actually, this exact same suggestions appears in https://stackoverflow.com/a/2654806/1832154

def is_instance_userdefined_and_newclass(inst):
    cls = inst.__class__
    if hasattr(cls, '__class__'):
        return ('__dict__' in dir(cls) or hasattr(cls, '__slots__'))
    return False

>>> class A: pass
... 
>>> class B(object): pass
... 
>>> a = A()
>>> b = B()
>>> is_instance_userdefined_and_newclass(1)
False
>>> is_instance_userdefined_and_newclass(a)
False
>>> is_instance_userdefined_and_newclass(b)
True
like image 134
mmgp Avatar answered Oct 23 '22 00:10

mmgp


I'm not sure about the "correct" way, but one easy way to test it is that instances of old style classes have the type 'instance' instead of their actual class.

So type(x) is x.__class__ or type(x) is not types.InstanceType should both work.

>>> class Old:
...     pass
...
>>> class New(object):
...     pass
...
>>> x = Old()
>>> y = New()
>>> type(x) is x.__class__
False
>>> type(y) is y.__class__
True
>>> type(x) is types.InstanceType
True
>>> type(y) is types.InstanceType
False
like image 25
Antimony Avatar answered Oct 22 '22 22:10

Antimony


This tells us True if it is.

if issubclass(checkthis, (object)) and 'a' not in vars(__builtins__):print"YES!"

The second argument is a tuple of the classes to be checked. This is easy to understand and i'm sure it works. [edit (object) to(object,) thanks Duncan!]

like image 21
noɥʇʎԀʎzɐɹƆ Avatar answered Oct 23 '22 00:10

noɥʇʎԀʎzɐɹƆ