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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:28:48+00:00 2026-05-27T21:28:48+00:00

General Python Question I’m importing a Python library (call it animals.py) with the following

  • 0

General Python Question

I’m importing a Python library (call it animals.py) with the following class structure:

class Animal(object): pass

class Rat(Animal): pass

class Bat(Animal): pass

class Cat(Animal): pass

...

I want to add a parent class (Pet) to each of the species classes (Rat, Bat, Cat, …); however, I cannot change the actual source of the library I’m importing, so it has to be a run time change.

The following seems to work:

import animals

class Pet(object): pass

for klass in (animals.Rat, animals.Bat, animals.Cat, ...): 
    klass.__bases__ = (Pet,) + klass.__bases__

Is this the best way to inject a parent class into an inheritance tree in Python without making modification to the source definition of the class to be modified?

Motivating Circumstances

I’m trying to graft persistence onto the a large library that controls lab equipment. Messing with it is out of the question. I want to give ZODB’s Persistent a try. I don’t want to write the mixin/facade wrapper library because I’m dealing with 100+ classes and lots of imports in my application code that would need to be updated. I’m testing options by hacking on my entry point only: setting up the DB, patching as shown above (but pulling the species classes w/ introspection on the animals module instead of explicit listing) then closing out the DB as I exit.

Mea Culpa / Request

This is an intentionally general question. I’m interested in different approaches to injecting a parent and comments on the pros and cons of those approaches. I agree that this sort of runtime chicanery would make for really confusing code. If I settle on ZODB I’ll do something explicit. For now, as a regular user of python, I’m curious about the general case.

  • 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-27T21:28:49+00:00Added an answer on May 27, 2026 at 9:28 pm

    Your method is pretty much how to do it dynamically. The real question is: What does this new parent class add? If you are trying to insert your own methods in a method chain that exists in the classes already there, and they were not written properly, you won’t be able to; if you are adding original methods (e.g. an interface layer), then you could possibly just use functions instead.

    I am one who embraces Python’s dynamic nature, and would have no problem using the code you have presented. Make sure you have good unit tests in place (dynamic or not ;), and that modifying the inheritance tree actually lets you do what you need, and enjoy Python!

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

Sidebar

Related Questions

In Python, and in general - does a close() operation on a file object
general question is i like to build logger class that writes to single log
This is more of a Python general question however in a context of django.
I am new to Python, and programming in general. While I think this question
It's likely that this is just a general Python Tkinter question, not necessarily a
I am working in Python, but this is a general design question so general
General tutorial or good resource on how to use threads in Python? When to
Emacs does not recognize my correct Python path. I think it is a general
General javascript question here, which would also be good to know how(if possible) to
I came here to ask a simple, very beginner question regarding python. I just

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.