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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:14:21+00:00 2026-06-11T10:14:21+00:00

I’m struggling to find a pythonic approach to the following class organization: I have

  • 0

I’m struggling to find a “pythonic” approach to the following class organization:

I have a base class with properties initialized in its constructor, for example:

class Animal(object):
    def __init__(self, class_, species, is_domesticated):
        self.class_ = class_
        self.species = species
        self.is_domesticated = is_domesticated

Then, when I subclass, I would like to “hard-code” one or more of these properties, like so:

class Mammal(Animal):
    def __init__(self, species, is_domesticated):
        Animal.__init__(self, 'Mammal', species, is_domesticated)

A Mammal is thus instantiated like so:

monkey = Mammal('Primate', false)

The problem is, I would like to use *args so as to leave any derived classes alone when altering the base class definition. Thus the definition of Mammal becomes:

class Mammal(Animal):
    def __init__(self, *args):
        Animal.__init(self, *(args + (class_='Mammal',)))

Which (needless to say) looks horrible. Some tips would be appreciated =)

  • 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-11T10:14:22+00:00Added an answer on June 11, 2026 at 10:14 am

    If you only have a fixed set of arguments in the base class, there isn’t much need to worry about variable arguments. Just do what you did in your first example and it’s fine. If you want to be able to randomly add arguments to the base class, but add them as positional arguments and without defaults, there’s no hope; you can’t just change the base class willy-nilly and expect all derived classes to keep working.

    However, there is a fairly common intermediate case where you might have a large set of attributes, various combinations of which may be passed to any class in the hierarchy. You might want to add new arguments to the base class, but they’ll have defaults so that derived classes don’t need to know about them explicitly; they’ll just gracefully degrade to the base-class default. In such a case it’s usually a better idea to use **kwargs rather than *args.

    class Animal(object):
        def __init__(self, **kwargs):
            self.class_ = kwargs['class_']
            self.species = kwargs['species']
            # etc.
    class Mammal(Animal):
        def __init__(self, **kwargs):
            Animal.__init__(self, class_="Mammal", **kwargs)
    

    This requires the arguments to passed by keyword:

    >>> Animal(class_='Fish', species='barracuda', is_domesticated=False)
    4: <__main__.Animal object at 0x0177ABF0>
    >>> Mammal(species="monkey", is_domesticated=False)
    5: <__main__.Mammal object at 0x0177AFB0>
    

    . . . but this is better if there are a lot of them, because no one will remember which order to pass them in if you have 10 different things getting passed in positionally. It also means that you can add new arguments easily; no one has to know where in the list to put the new ones, they can just add them anywhere by keyword.

    In Python 2 you have to manually extract the kwargs as I did above. In Python 3 you can use keyword-only arguments to make this even easier.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.