0

I read from Learning Python (5th edition) that (on Page 1364, Chapter 40):

In Python 2.X, new-style classes inherit from object, which is a subclass of type; classic classes are instances of type and are not created from a class.

However,

issubclass(object, type)

gives me

False

in Python 2.7.

So, it seems that the author made a false statement that object is a subclass of type, or am I missing anything?

yuanlu0210
  • 193
  • 6

2 Answers2

2

object is not a subclass of type, that would make it a metaclass. Instead object is an instance of type type.

The function issubclass checks if a given class inherits from another.

class A:
    pass

class B(A):
    pass

print(issubclass(B, A)) # True

It does not check if an instance os of a given type. To verify if object is indeed of type type, you want to use isinstance.

print(isinstance(object, type)) # True
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
0

Use isinstance(). In python 2.7.10

print object
print isinstance(object, type)
print issubclass(object, type)
print object.__class__

outputs

<type 'object'>
True
False
<type 'type'>

type is a metaclass as explained here

deadvoid
  • 1,270
  • 10
  • 19