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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:47:18+00:00 2026-05-17T02:47:18+00:00

I’ve read some tutorials on Python metaclasses. I’ve never used one before, but I

  • 0

I’ve read some tutorials on Python metaclasses. I’ve never used one before, but I need one for something relatively simple and all the tutorials seem geared towards much more complex use cases. I basically want to create a template class that has some pre-specified body, but takes its base class as a parameter. Since I got the idea from C++/D templates, here’s an example of what the code I want to write would look like in C++:

template<class T>
    class Foo : T {
        void fun() {}
    }
  • 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-17T02:47:19+00:00Added an answer on May 17, 2026 at 2:47 am

    Although it certainly can be done with metaclasses, you can do what you want without them because in Python classes are themselves objects. The means that—surprisingly—essentially nothing more than an almost one-to-one translation of the C++ code is required. Besides being relatively uncomplicated because of this, it’ll also work without modification in both Python 2 & 3.

    def template(class_T):
        """Factory function to create subclasses of class_T."""
    
        class Foo(class_T):
            def fun(self):
                print('%s.fun()' % self.__class__.__name__)
    
        Foo.__name__ += '_' + class_T.__name__  # rename the subclass to reflect its heritage
        return Foo
    
    class Base1:
        def bar(self):
            print('Base1.bar()')
    
    class Base2:
        def bar(self):
            print('Base2.bar()')
    
    Foo_Base1 = template(Base1)
    print('Foo_Base1 base classes: {}'.format(Foo_Base1.__bases__))
    
    Foo_Base2 = template(Base2)
    print('Foo_Base2 base classes: {}'.format(Foo_Base2.__bases__))
    
    subclass1 = Foo_Base1()
    subclass1.fun()
    subclass1.bar()
    subclass2 = Foo_Base2()
    subclass2.fun()
    subclass2.bar()
    

    Output:

    Foo_Base1 base classes: (<class __main__.Base1 at 0x00A79C38>,)
    Foo_Base2 base classes: (<class __main__.Base2 at 0x00A79DC0>,)
    Foo_Base1.fun()
    Base1.bar()
    Foo_Base2.fun()
    Base2.bar()
    

    The code in the (unimaginatively-named) template() function is an example of what is commonly called a class factory or an implementation of the Factory pattern. So, incidentally, you might find my answer to the question What exactly is a Class Factory? informative.

    Edit: Added code to create different class names for each subclass returned—which was inspired by @aaronasterling‘s insight (in a now deleted comment) about potential confusion when debugging if the class manufactured always has the same name.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.