0

Why object will be passed in certain cases.What is the need passing the object as the class argument.I know that it would be useful at the time of decorators but other than that any other need for this

 class MyClass(object):
       def __init__(self):   
          self.numbers = [1,2,3,4,54] 
       def __contains__(self, key):
           return key in self.numbers 

The second code also works without object argument

 class MyClass:
       def __init__(self):   
          self.numbers = [1,2,3,4,54] 
       def __contains__(self, key):
           return key in self.numbers 
Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • The parameter passed to the class is the class which it is inheriting from. I believe it is good practice to always inherit from the base Python object, as your first code example does. [This question](http://stackoverflow.com/questions/4015417/python-class-inherits-object) asks the same thing. – kevintodisco Mar 08 '12 at 06:23
  • The first is called new style class, the second is called old style class. Checkout http://stackoverflow.com/q/54867/205528 for the differences – Kien Truong Mar 08 '12 at 06:26

2 Answers2

2

Classes that derive from object become new-style classes.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

In the first example, you are not passing an object as an argument, you are making MyClass inherit from the class object. The first, inheriting from object, is making MyClass a new style class.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621