-3

Trying to understand when to put the object argument in a class def and when not to. I have not found much about this on the web. Every time i try to use inheritance this causes major issues and i get all screwed up. Can someone explain please? Thanks

class cats():
def __init__(cats_name):
self.cats_name = cats_name

class cats(object):
def __init__(cats_name):
self.cats_name = cats_name

It seems like there is an issue when all your child sub classes have the object also. Python gets annoyed that there is more then one. So then i end up taking out the "object" from the other classes but that has side effects also.

Like for example, if you wanted to use one of those subclasses on its own later, now it has no object. Im missing something fundamental here.

All i know is "object" is like the base python class that lets your class become an object? Is that even right?

1 Answers1

1

You never need to explicitly inherit from object in Python 3. That's a holdover from Python 2, where

class cats:
    ...

defined an old-style class and

class cats(object):
    ...

defined a new-style class. In Python 3, there are no old-style classes anymore; only new-style classes are available.

Unless you are maintaining Python 2 code, the distinction isn't one to worry about. If you are maintaining Python 2 code, the rule is simple: always inherit from object if there is no other type to inherit from. There is no reason to define new old-style classes.

(And no, I am not even considering the possibility of having to write code in 2022 for versions of Python older than 2.2.)

chepner
  • 497,756
  • 71
  • 530
  • 681