Why does an object inherit?

Asked 2 years ago, Updated 2 years ago, 137 views

Why do you inherit an object by writing (object) after its name when declaring a class? Is there a difference that some codes use that and others don't?

class MyClass(object):
    # Class code

class python object inheritance newstyle

2022-09-22 14:44

1 Answers

The difference between the new-style class and the old-style class. Class styles are divided into the following, depending on the Python version:

Python 3.x:

Python 2.x:

Old-style and new-style began to be distinguished from Python 2.2.

Old-style, which was used before that, is so old that it doesn't exist from 3.x When defining a base class in python3.x, it is okay not to indicate the object.

The problem occurs when writing this 3.x code to 2.x code. If 2.x does not display objects, this class is considered old-style.

Old-style class and new-style class are very different This can cause unexpected problems for the program Even if it's annoying, please make sure to mark (object) for compatibility.


2022-09-22 14:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.