0

in Python 3 we don't have to inherit from object as it's inherited implicitly.

class A() #python 3 inherits from object

However in Python 2 we have to explicitly inherit from object:

class A(object) #python 2 

So at the one hand side, we eventually want to move to Python 3, which would be a point for not inheriting from object. Reading this thread: Python class inherits object. gives me the feeling, it can be eventually harmfull but not really. At the other hand we don't want to mess too much with hacky stuff.

So what is the way to go, if you are planning to switch to Python 3 soon and want to have a clean inheritance chain?

Community
  • 1
  • 1
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • 1
    Do you mean `object` or `qobject` above. You refer to both, which do you mean? – Martin Bonner supports Monica Mar 30 '16 at 08:53
  • Oh thanks for correcting me, its been late...let me correct that. – user1767754 Mar 30 '16 at 08:55
  • U should change all of your code to the new object with class A(object) in python 2 firstly. And than in python 3 u can hold class A(object) too. Very important: u should check if super(...) still works. I had many problems with super() by conversion my codes from python 2 to python 3. Another problems, which i had: print(), str-unicode, ... – qvpham Mar 30 '16 at 09:05

1 Answers1

2

Not sure why you think it would be harmful. On Python3 inheriting from object or not changes nothing, so you can keep it.

Here's an example if you don't trust me:

class A:
    pass
class B(object):
    pass
a = A()
print(A.mro())
print(B.mro())

[<class '__main__.A'>, <class 'object'>]
[<class '__main__.B'>, <class 'object'>]

(mro stands for "Method resolution order")

Dimitri Merejkowsky
  • 1,001
  • 10
  • 12
  • Yes, i've posted the article where this is described in detail, it's more about the fact, if there is any harm in not deriving from object in python 2 – user1767754 Mar 30 '16 at 09:08
  • @user1767754 In Python2, classes deriving from object are called new style classes and are more compatible with Python 3 classes. Classes not deriving from object are called old style classes. Please read Python manual for details, it would be too long for an SO post. – Serge Ballesta Mar 30 '16 at 09:30