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

后端 未结 1 1756
忘了有多久
忘了有多久 2021-01-02 09:56

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 the

相关标签:
1条回答
  • 2021-01-02 10:37

    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).
    0 讨论(0)
提交回复
热议问题