The Python Oracle

Class with Object as a parameter

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4

--

Chapters
00:00 Class With Object As A Parameter
00:25 Answer 1 Score 37
00:56 Answer 2 Score 4
01:18 Accepted Answer Score 152
02:17 Answer 4 Score 3
02:27 Thank you

--

Full question
https://stackoverflow.com/questions/7375...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 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.




ANSWER 2

Score 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.




ANSWER 3

Score 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.




ANSWER 4

Score 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”.