Python class inherits an object

0 votes
Is there any reason for a class declaration to inherit from object?

I just found some code that does this and I can't find a good reason why.

class MyClass(object):
    # class code follows...
Apr 17, 2018 in Python by aryya
• 7,440 points

1 answer to this question.

0 votes
Python 3.x:
class MyClass(object): = new-style class
class MyClass: = new-style class (implicitly inherits from object)

Python 2.x:
class MyClass(object): = new-style class
class MyClass: = OLD-STYLE CLASS

When defining base classes in Python 3.x, you’re allowed to drop the object from the definition. However, this can open the door for a seriously hard to track problem…

Python introduced new-style classes back in Python 2.2, and by now old-style classes are really quite old. Discussion of old-style classes is buried in the 2.x docs, and non-existent in the 3.x docs.

The problem is, the syntax for old-style classes in Python 2.x is the same as the alternative syntax for new-style classes in Python 3.x. Python 2.x is still very widely used (e.g. GAE, Web2Py), and any code (or coder) unwittingly bringing 3.x-style class definitions into 2.x code is going to end up with some seriously outdated base objects. And because old-style classes aren’t on anyone’s radar, they likely won’t know what hit them.

So just spell it out the long way and save some 2.x developer the trouble.
answered Apr 17, 2018 by anonymous

Related Questions In Python

0 votes
1 answer

Python class inherits object

Python 3.x: class MyClass(object): = new-style class class MyClass: = new-style ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,140 points
0 votes
1 answer

Size of an object in Python

Use sys.getsizeof() function: >>> import sys >>> s = ...READ MORE

answered May 25, 2018 in Python by Nietzsche's daemon
• 4,260 points
0 votes
1 answer

Can we access Outer class member from an Inner class in Python?

If you want to access outer class ...READ MORE

answered Feb 13, 2019 in Python by Omkar
• 69,150 points
0 votes
1 answer

Python: For loop to iterate class object

Refer to the below code: class Try: ...READ MORE

answered May 22, 2019 in Python by Raj
0 votes
1 answer
0 votes
1 answer

How to insert an object at a given index in Python?

Hi, @Roshni, I will explain to you with ...READ MORE

answered Jun 24, 2020 in Python by Gitika
• 65,970 points
0 votes
1 answer

How to check the memory usage of an object in python

Hi, @Roshni, You can use this code below: import ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,970 points
0 votes
1 answer

How to know if an object has an attribute in Python?

Try hasattr(): if hasattr(a, 'property'): The ...READ MORE

answered Dec 2, 2020 in Python by Gitika
• 65,970 points
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,780 points
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous