11

When you declare a class in python, I often see (object) written next to the class name.

class someClass(object):
    def __init__(self, some_variable):
        ...
    ...

Is this same as writing below?

class someClass: # didn't write (object) here.
    def __init__(self, some_variable):
        ...
    ...

I don't really see any difference in terms of how they function. Is it just a way to clarify that someClass is a subclass of object? and is it a good practice to explicitly write object when I make a class?

chanpkr
  • 895
  • 2
  • 10
  • 21
  • 1
    I like @jwodder's answer for the explanation. As far as best practices go, I tend to explicitly use `(object)` just so my code can be used with both Python 2.x and 3 with minimal confusion. – austin Oct 27 '13 at 03:14

2 Answers2

14

In Python 2, making someClass a subclass of object turns someClass into a "new-style class," whereas without (object) it's just a "classic class." See the docs or another question here for information on the differences between them; the short answer is that you should always use new-style classes for the benefits they bring.

In Python 3, all classes are "new-style," and writing (object) is redundant.

Community
  • 1
  • 1
jwodder
  • 54,758
  • 12
  • 108
  • 124
1

In python 3.x, they are the same, when you declare:

class C:
    def __init__(self):
        ...

it inherits from object implicitly.

For more information visit this.

Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73