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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:31:10+00:00 2026-05-28T18:31:10+00:00

I have a number of classes which are wrapped by other classes to add

  • 0

I have a number of classes which are wrapped by other classes to add new functionality.

Unfortunately, the wrapper classes don’t implement pass through functions for the classes they are wrapping, so the wrapper cannot be used interchangeably with the original class.

I would like to dynamically create classes that contain the functionality of both the wrapper and the original class.

The idea I had was to create a mix-in class and use a factory to apply it to the existing class to create a new dual use class dynamically. This should allow me to write the mix-in once, and for the mixed class to be used to provide either the original functionality or the enhanced functionality from from the mix-in via one object.

This is the sort of thing I’m after:

class A:
    def __init__(self):
        self.name = 'A'

    def doA(self):
        print "A:", self.name


class B(A):
    def __init__(self):
        self.name = 'B'

    def doB(self):
        print "B:", self.name


class C(A):
    def __init__(self):
        self.name = 'C'

    def doC(self):
        print "C:", self.name


class D:
    def doD(self):
        print "D:", self.name


class BD(B,D):
    pass


def MixinFactory(name, base_class, mixin):
    print "Creating %s" % name
    return class(base_class, mixin)     # SyntaxError: invalid syntax

a, b, c, d, bd = A(), B(), C(), D(), BD()

bd2 = MixinFactory('BD2', B, D)()
cd = MixinFactory('CD', C, D)()

a.doA()     # A: A

b.doA()     # A: B
b.doB()     # B: B

c.doA()     # A: C
c.doC()     # C: C

bd.doA()    # A: B
bd.doB()    # B: B
bd.doD()    # D: B

bd2.doA()   # A: B
bd2.doB()   # B: B
bd2.doD()   # D: B

cd.doA()    # A: C
cd.doC()    # C: C
cd.doD()    # D: C

The problem is that obviously that you can’t just return a class from a function. Ignoring the syntax error though, the above code does show what I’m trying to achieve.

I had a play with the three argument variant of type() but couldn’t get that to work, so I’m not sure if that is the right approach.

I assume that creating a mix-in factory of this type is possible in Python, so what do I need to understand to implement it?


As Niklas R commented, this answer to the question Python dynamic inheritance: How to choose base class upon instance creation? provides the solution to my query, but Ben‘s answer here provides a better explanation of why.

  • 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-28T18:31:11+00:00Added an answer on May 28, 2026 at 6:31 pm

    Actually you can return a class from a function. Your syntax error is that you’re using the class keyword as if it were a function you can invoke.

    Remember that all a class block for is create a new class and then bind the name you chose to it (in the current scope). So just put a class block inside your function, then return the class!

    def mixinFactory(name, base, mixin):
        class _tmp(base, mixin):
            pass
        _tmp.__name__ = name
        return _tmp
    

    As noted by ejucovy, you can also call type directly:

    def mixinFactory(name, base, mixin):
        return type(name, (base, mixin), {})
    

    This works because it’s (usually) what a class block actually does; it collects all the names you define in the class block into a dictionary, then passes the name of the class, a tuple of the base classes, and the dictionary on to type to construct the new class.

    However, type is nothing more than the default metaclass. Classes are objects like everything else, and are instances of a class. Most classes are instances of type, but if there’s another metaclass involved you should call that instead of type, just as you wouldn’t call object to create a new instance of your class.

    Your mixin (presumably) doesn’t define a metaclass, so it should be compatible with any metaclass that is a subclass of type. So you can just use whatever the class of base is:

    def mixinFactory(name, base, mixin):
        return base.__class__(name, (base, mixin), {})
    

    However, S.Lott’s comment really seems like the best “answer” to this question, unless your mixin factory is doing anything more complicated than just creating a new class. This is much clearer AND less typing than any of the dynamic class creation variants proposed:

    class NewClass(base, mixin):
        pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of classes which have private member variables that implement IDisposable
I've got a number of classes which have a relationship to other classes for
I have developed a number of classes which manipulate files in Java. I am
I have a number of CSS attributes and Classes which get applied based on
I've got quite a number of classes, which have got the standard set and
I have several obj-c classes, each of which require a number of constants that
I have a number of classes and they are quite close to each other
I have a problem I am working on. I have a number classes which
I have a number of data classes, which share an abstract base class so
I have a number of classes which all share the same methods, only with

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.