154

I'm trying to translate some python code to scala code. So I'm a total noob in Python.

But why do some classes have object as a parameter but never explicitly use it? What's the reasoning for having it as a parameter in the first place?

Example:

class Table(object)

Thank you for your time.

mythicalprogrammer
  • 4,647
  • 6
  • 35
  • 42
  • 1
    possible duplicate of [Old style and new style classes in Python](http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python) – JBernardo Sep 11 '11 at 05:19

6 Answers6

152

In Python2 this declares Table to be a new-style class (as opposed to "classic" class). In Python3 all classes are new-style classes, so this is no longer necessary.

New style classes have a few special attributes that classic classes lack.

class Classic: pass
class NewStyle(object): pass

print(dir(Classic))
# ['__doc__', '__module__']

print(dir(NewStyle))
# ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Also, properties and super do not work with classic classes.

In Python2 it is a good idea to make all classes new-style classes. (Though a lot of classes in the standard library are still classic classes, for the sake of backward-compatibility.)

In general, in a statement such as

class Foo(Base1, Base2):

Foo is being declared as a class inheriting from base classes Base1 and Base2.

object is the mother of all classes in Python. It is a new-style class, so inheriting from object makes Table a new-style class.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • can you redirect me to a source that mentions object is not necessary as parameter in Python3? I cannot seem to find it. Thank you – Snow Nov 06 '17 at 12:36
  • 2
    It's been mentioned many times on StackOverflow ([here, for instance](https://stackoverflow.com/a/16193572/190597)), but I haven't been able to find a simple declaration of this in the docs. It is shown implicitly (by the omission of `object` from the list of class bases) [in the tutorial](https://docs.python.org/3/tutorial/classes.html#class-definition-syntax). – unutbu Nov 06 '17 at 13:44
  • 1
    The [Python3 docs](https://docs.python.org/3/glossary.html#term-new-style-class) also state that a "new-style class" is the "old name for the flavor of classes **now used for all class objects**" (my emphasis). And [the Python2 docs state](https://docs.python.org/2/glossary.html#term-new-style-class) that a "new-style class" is any class which inherits from `object`. The two statements put together imply that you don't need to include `object` in the list of class bases in Python3 since all classes are new-style classes in Python3. – unutbu Nov 06 '17 at 13:44
37

The Table class is extending a class called object. It's not an argument. The reason you may want to extend object explicitly is it turns the class into a new-style class. If you don't explicitly specify it extends object, until Python 3, it will default to being an old-style class. (Since Python 3, all classes are new-style, whether you explicitly extend object or not.)

For more information on new-style and old-style classes, please see this question.

Community
  • 1
  • 1
icktoofay
  • 126,289
  • 21
  • 250
  • 231
4

class Table and class Table(object) are no different for Python.

It's not a parameter, its extending from object (which is base Class like many other languages).

All it says is that it inherits whatever is defined in "object". This is the default behaviour.

Devraj
  • 3,025
  • 24
  • 25
  • 4
    Classes automatically extending `object` is default behaviour in Python 3 only. In python 2, a class extending object has attributes that a class that doesn't extend object will not have. – Lewis Ellis Sep 11 '11 at 16:07
3

Just a note that the “new-style" vs “old-style” class distinction is specific to Python 2.x; in 3.x, all classes are “new-style”.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13
2

object is the most base type of class object defined in python. Attributes of object can be seen as below

**>>> dir(object)

['class', 'delattr', 'doc', 'format', 'getattribute', 'hash', 'init', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook']**

So Table(object) is just inheritance.!

0

1)Class name (object): 2) class name: They both are same but first one quite better in terms of writing,it look better while inheriting other classes to another,i t looks homogeneous.

How same ? So,every thing in Python is come under object means every thing in Python has property of object,if write or don't it will understand it. In first we explicitly tell it in second we didn't.