Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7906785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:08:09+00:00 2026-06-03T11:08:09+00:00

Possible Duplicate: Why aren't Python's superclass init methods automatically invoked? For example: class Pet(object):

  • 0

Possible Duplicate:
Why aren't Python's superclass init methods automatically invoked?

For example:

class Pet(object):

    def __init__(self, name, species):
       self.name = name
       self.species = species

    def getName(self):
     return self.name

    def getSpecies(self):
     return self.species

    def __str__(self):
     return "%s is a %s" % (self.name, self.species)

class Dog(Pet):

   def __init__(self, name, chases_cats):
      Pet.__init__(self, name, "Dog")
      self.chases_cats = chases_cats

   def chasesCats(self):
     return self.chases_cats

As you can see Dog inherits from pet. I understand the code perfectly fine. But why must we call the init for pet in the Dog class? Why isn’t just calling it as in the first line of the dog class enough ( class Dog(Pet) )? It seems to only create messier code. It kind of kills the point of inheritance in Python to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:08:12+00:00Added an answer on June 3, 2026 at 11:08 am

    Having the language force the super class to initialize before or after means that you lose functionality. A subclass may depend on a superclass’s initialization to be run first, or vice versa.

    In addition, it wouldn’t have any way of knowing what arguments to pass — subclasses decide what values are passed to an initializer, which gives more flexibility over automatically passing all arguments.

    An alternative method of initializing the super-class is by using super, though this will actually initialize the super-class of self dynamically, by looking it up in the object’s __mro__ (method resolution order)

    In python 2:

    super(self, Dog).__init__(self, name, "Dog")
    

    In python 3, you can further reduce the syntax, and avoid repeating yourself:

    super().__init__(self, name, "Dog")
    

    Edit:

    Since you’re not actually using the name argument passed to dog, you can further reduce some syntax, by accepting arbitrary keyword and positional arguments in the Dog initializer, and passing them up the initialization chain:

    class Dog(Pet):
    
       def __init__(self, chases_cats, *args, **kwargs):
          Pet.__init__(self, *args, species="Dog", **kwargs)
          self.chases_cats = chases_cats
    
    class Pet(object):
    
        def __init__(self, name, species, *args, **kwargs):
           self.name = name
           self.species = species
    

    In your scenario, the initializer might be simple enough to not need this, but it’s useful to know for when you have more arguments, and/or a deeper class hierarchy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: extension method requires class to be static Why aren't C# static class
Possible Duplicate: How do I use define_method to create class methods? I am trying
Possible Duplicate: Why aren't Java Collections remove methods generic? Does anyone know why MyDamnedUnclearList.remove()
Possible Duplicate: Where do the Python unit tests go? Are unit tests kept in
Possible Duplicate: .NET Controls: Why aren’t all calls thread-safe? This question is not about
Possible Duplicate: Flatten (an irregular) list of lists in Python I'm trying to use
Possible Duplicate: What are the differences between struct and class in C++ http://www.cplusplus.com/reference/std/typeinfo/type_info/ I
Possible Duplicate: Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?
Possible Duplicate: Why aren't static const floats allowed? Is there any reason why this
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.