简体   繁体   中英

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM