0

I have seen both examples whereby developers inherit object and some do not. Is there any difference between the two approaches?

# case 1

class NoObj:
    pass


# case 2

class Obj(object):
    pass
alani
  • 11,960
  • 2
  • 10
  • 22
InfoLearner
  • 14,232
  • 18
  • 69
  • 117

2 Answers2

2

This only matters if you are using Python 2, class Foo() will create an old-style class so I suggest you always use class Foo(object): to create a new-style class.

But if you are using Python 3, class Foo: is the same as class Foo(): and class Foo(object):, so you can use any of those because all of them will create a new-style class. I personally use the first one.

Julio Suriano
  • 131
  • 1
  • 1
  • 7
0

I have same question some time ego and do some quick search to found out, that in Python 3 you don't need to inherit object class any more. This is the stuff called "new style classes", that type of classes always inherits to object: https://wiki.python.org/moin/NewClassVsClassicClass