3

Why does Python see these classes as different data types?

>>> class A:
...    pass
...
>>> class B(object):
...     pass
...
>>> a = A()
>>> b = B()
>>> type(A)
<type 'classobj'>
>>> type(B)
<type 'type'>
>>> type(a)
<type 'instance'>
>>> type(b)
<class '__main__.B'>

I'm pretty new. So I don't really understand why it sees all of this as different data types. They are both classes so it seems as though they should be the same.

1

1 Answer 1

7

You're using Python 2.

Python 2 allows classes that don't inherit from object, which was added in version 2.2. They behave differently from "new-style classes" in a few ways, and you've found a couple.

There's no reason for the different behaviour other than to retain backward-compatibility, that is to ensure that code written for old-style classes continues to work in new releases of Python 2.

Python 3 is not backward-compatible and does not have old-style classes. If you wrote the same code in Python 3, then A would inherit from object even though you don't say so explicitly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.