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.

asked Oct 29, 2013 at 10:20
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.

answered Oct 29, 2013 at 10:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.