Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between class foo , class foo() and class foo(object)?

I noticed all 3 -> class foo, class foo() and class foo(object) can be used but i am confused as to what is the difference between these 3, if there is any? (I mean in properties mainly, python3)

like image 647
Zerg Overmind Avatar asked Feb 15 '17 19:02

Zerg Overmind


1 Answers

Let's break them down:

  1. class foo:

    • Python 3: It's usually the way to go. By default, Python adds object as the base class for you.
    • Python 2: It creates an old style classobj that will cause you all sorts of headaches.
  2. class foo():

    • Python 3 and Python 2: Similar to class foo for both Python versions, trim it off, it looks ugly and makes no difference.
  3. class foo(object):
    • Python 3 and Python 2: In both Pythons, results in a new style class that has all the goodies most know. People usually use this form when writing code that might be used in Python 2 too, explicitly inheriting from object causes the class to be new style in Python 2 and makes no difference in 3 (apart from some extra typing).
like image 67
Dimitris Fasarakis Hilliard Avatar answered Oct 19 '22 09:10

Dimitris Fasarakis Hilliard