20

Assume a simple class:

class MyClass(object):
    pass

.
.
.
m = MyClass
print type(m) # gets: <type 'classobj'>
# if m is classobj, how can i check a variable is class object?

My question is: how can i check a variable is a class object?

a simple solution:

if str(type(m)) == "<type 'classobj'>":
    # do something

But i think there is at least one classic way to check that.

pylover
  • 7,670
  • 8
  • 51
  • 73
  • 1
    The code you showed above will not print `` for `type(m)`, because `m` is a new-style class. It will print ``. You'll only get `` for classic classes. – abarnert Apr 05 '13 at 19:02

5 Answers5

42

Use inspect:

import inspect
print inspect.isclass(obj)
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
26

In 2.x, a class object can be be a type (new-style classes) or a classobj (classic classes). The type type is a builtin, but the classobj type is not. So, how do you get it? That's what the types module is for.

isinstance(MyClass, (types.TypeType, types.ClassType))

In 3.x, you don't need to worry about classic classes, so:

isinstance(MyClass, type)

Even if you want to compare the types directly, you should never compare any objects by their str. Compare:

>>> class MyClassicClass:
...     pass
>>> str(type(MyClassicClass)) == "<type 'classobj'>"
True
>>> str("<type 'classobj'>") == "<type 'classobj'>"
True

You can almost always just compare objects directly:

>>> type(MyClassicClass) == types.ClassType
True
>>> "<type 'classobj'>" == types.ClassType
False

(And in the incredibly rare cases where you really do need to compare the string representation for some reason, you want the repr, not the str.)

abarnert
  • 354,177
  • 51
  • 601
  • 671
8

A new-style class (derived explicitly from object as in your example, or implicitly in Python 3) is not of type classobj. Only old-style classes are classobjs. So for your example class as written, you could simply check to see if MyClass is an instance of type, as all new-style classes are:

isinstance(MyClass, type)

Because all new-style classes are derived from object you can also do:

issubclass(MyClass, object)

I would advise against:

type(MyClass) == type

This will fail if the type of a class is not type, but a metaclass. Although there may be use cases for that, of course.

If you really need to see if you've got an old-style class, you can import the types module and use types.ClassType, or just declare an old-style class and see if the class you're testing is of the same type:

class OldStyleClass:
   pass
OldStyleClass = type(OldStyleClass)

isinstance(MyClass, OldStyleClass)
kindall
  • 178,883
  • 35
  • 278
  • 309
3

Is this what you're looking for?

>>> class A(object):
...    pass
>>> isinstance(A, type)
True

>>> isinstance("asd", type)
False

But works only for classes inherited from object.

Piotr Hajduga
  • 608
  • 4
  • 13
0

Sometimes the next is working (if you want to determine how to print the object):

def is_class(o):
    return hasattr(o, '__dict__')
sergzach
  • 6,578
  • 7
  • 46
  • 84
  • most of objects including classes, has this attribute – pylover Sep 15 '16 at 07:10
  • @pylover Excuse me, I answered to another question: "How to determine if a variable is a class instance". I'm agree with you. But could you give an example of objects which are not classes and have this attribute? – sergzach Sep 17 '16 at 20:56
  • No, the question is `How to check a variable is class **OBJECT** or not`, not INSTANCE – pylover Sep 18 '16 at 08:25
  • Your method always return true for either class or instance – pylover Sep 18 '16 at 08:27
  • @pylover `i = int(5)` is an `object`, because `issubclass(int, object)` is `True`, but the method returns `False` for ints because `int` is not a real user-defined class. so. its not an instance of a real class. – sergzach Sep 18 '16 at 19:46
  • you don't get the question well as well. – pylover Sep 19 '16 at 08:33