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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:50:02+00:00 2026-05-30T02:50:02+00:00

Here’s the coding problem I am trying to solve… I have a base class,

  • 0

Here’s the coding problem I am trying to solve… I have a base class, let’s say Animal, and it has two subclasses, say Dog and Cat. My class Animal has a method, make_baby(), that both Dog and Cat will inherit. The trick I’m having trouble pulling off is that I want the return value to be a new instance of the subclass that called the function but with different attribute values, i.e. Dog.make_baby() should return a new Dog and Cat.make_baby() will return a new Cat.

I previously tried returning “type(self)()”, but this is no good because type() return a type object, not a class.

Here is the full example code:

Class Animal():
  def __init__(self, color):
    self.color = color
  def make_baby():
    new_color = rand_color # a randomly chosen color
    return #??? new class of the same type that called the method

Class Dog(Animal):
  def pet():
    print '*pant*'

Class Cat(Animal):
  def pet():
    print 'purrr'

So I’d like to avoid writing a make_baby() method for Dogs and Cats because the idea is that the method is exactly the same except for the returned class. I’d also like to avoid a bunch of if statements because I’d like to make and arbitrarily large number of subclasses to Animal.

  • 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-30T02:50:03+00:00Added an answer on May 30, 2026 at 2:50 am

    You wrote:

    this is no good because type() return a type object, not a class.

    A type is a class, if you’re using new-style classes. If you’re using Python 3, you’re set; all Python 3 classes are “new-style.” If you’re using Python 2.x, derive your class from object (or from something else that derives from object, like any built-in Python type).

    But what you really want here is a class method, where you get a reference to the class passed in automatically.

    class Animal(object):
    
      def __init__(self, color):
        self.color = color
    
      @classmethod
      def make_baby(cls):
        return cls(rand_color)   # randomly-chosen color
    

    You can call it on the class (e.g. Animal.make_baby() or Dog.make_baby()) or on an instance; either way the method still receives the class as the first argument.

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

Sidebar

Related Questions

Here's a coding problem for those that like this kind of thing. Let's see
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is my problem : I have a post controller with the action create.
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
Here is my code (Say we have a single button on the page that
Here is what I am trying to achieve in PHP: I have this string:
Here is my problem...I have a page that loads a list of clients and

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.