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,460 points
1,871 views

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

Explanation:
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,020 points
802 views
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
1,165 views
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,220 points
13,046 views
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
8,144 views
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,770 points
1,291 views
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,770 points
3,444 views
0 votes
1 answer

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

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

answered Dec 2, 2020 in Python by Gitika
• 65,770 points
568 views
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
3,452 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP