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 6049745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:36:41+00:00 2026-05-23T07:36:41+00:00

class Animal(object): def __init__(self, nlegs=4): self.nlegs = nlegs class Cat(Animal): def __init__(self, talk=’meow’): self.talk

  • 0
class Animal(object):
    def __init__(self, nlegs=4):
        self.nlegs = nlegs
    
class Cat(Animal):
    def __init__(self, talk='meow'):
        self.talk = talk

class Dog(Animal):
    def __init__(self, talk='woof'):
        self.talk = talk
  1. Why does my cat tom = Cat() not have an nlegs attribute?
  2. Should we explicitly call Animal.__init__() from within Cat.__init__, or should we use super here?
  3. What if we want to create a cat with 5 legs, should we add additional arguments to the Cat.__init__ interface?
  • 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-05-23T07:36:42+00:00Added an answer on May 23, 2026 at 7:36 am

    To build on what everyone else has said, yes, you’ll need to call the parent’s __init__ method.

    It’s generally best to use super. However, in certain cases (particularly when you’re inheriting from multiple classes) it can be a big gotcha. I’ll avoid going into detail, there are no shortage of various articles which discuss it. (Also, there are some oddities with some of the other “special” functions. For example, you can do super(SomeCls, self).__getitem__(5) but super(SomeCls, self)[5] won’t work.)

    As a simplistic example of why it’s a good idea to use it, you could make Dog and Cat inherit from Mammal (which inherits from Animal) and not have to change places in your code other than which class Dog and Cat inherit from.

    As for why your tom instance doesn’t have tom.nlegs, it’s because you haven’t called Animal‘s __init__ method.

    Also remember that not everything needs to be set at initialization time. For this example, it makes more sense not to set things like nlegs in the __init__ method. Instead, just set it directly in the class. E.g.

    class Mammal(object):
        nlimbs = 4
        def __init__(self):
            print "I'm a mammal!"
    
    class Cat(Mammal):
        def __init__(self, color="calico"):
            self.color = color
            super(Cat, self).__init__()
            print "I have {0} legs I am {1}".format(self.nlimbs, self.color)
    
    class FiveLeggedCat(Cat):
        nlimbs = 5
    

    Basically, if something is likely to change from instance to instance (e.g. the color of the cat) or needs to be done at initialization (e.g. opening a file), then it probably should be set in __init__.

    Otherwise, if it’s something we want to be the same for any instance of the class, it can be cleaner to set it directly in the class definition.

    Also, attributes set this way will be available to documentation tools (e.g. the built-in help function), whereas attributes set at initialization won’t be.

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

Sidebar

Related Questions

#Maps.py class Maps(object): def __init__(self): self.animals = [] self.currently_occupied = {} def add_animal(self, name):
Say I have models: class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print
Suppose I have class animal and classes cat and dog extending it. I want
say I have this class: class animal { function noise() { print 'woof'; }
Animal is the parent class of Cat class. public static void main (String[] args)
I have an Animal class that implements IDomainObject . There is another class, Cat
I have a collection of polymorphic objects, all derived from my Animal class: Cat,
I have a before_create callback that sets an error inside an object. class Animal
I have a class Dog extends Animal . Then I call the hashCode() method
public class Animal { public void eat() { System.out.println("I eat like a generic Animal.");

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.