Difference between class foo , class foo() and cla

2020-02-28 06:04发布

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)

1条回答
叛逆
2楼-- · 2020-02-28 06:21

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).
查看更多
登录 后发表回答