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

The Archive Base Latest Questions

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

In my code, I generate new python classes at runtime. For some of them,

  • 0

In my code, I generate new python classes at runtime. For some of them, I want to generate the python code, just as if I wrote these classes in a .py file.

Let’s say that I created dynamically a class A:
type('A', (), {'bar':True} which is equivalent to the code:

class A(object): 
    bar=True

What I want is to generate this equivalent code from my dynamic class.
I’m trying to implement a function “generate_A_code”

kls_A = type('A', (), {'bar':True}
kls_A.generate_A_code()

Hope this helps a bit.

Thanks

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

    Generating the Python code from the class object itself is practically impossible. You need to save the instructions for generating the class in another way.


    The best way may be having a function to make the class, and importing & calling it in the generated code. So the generated code would look like this:

    kwargs = {answer=42, name='foo'}
    
    import class_maker
    SomeClass1 = class_maker.make_class(**kwargs)
    

    The other option is generate the Python code you want directly, exec it to make the class, and then save it with the class.

    code = '''class MyClass:
        pass
    '''
    
    the_locals = {}
    exec(code, globals(), the_locals)
    MyClass = the_locals['MyClass']
    MyClass._code = code
    

    As always with exec, be very careful using it. It can run any Python code. Think hard if there’s any way to do whatever you need to do in a different way. People will shout at you for using exec. (But even the Python standard library uses it for dynamic classes sometimes.)

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

Sidebar

Related Questions

I use this code to generate a random number. Random R = new Random(0);
The following code will generate a link to the page I want to get
I'm trying to generate code coverage reports with EMMA using tests of which some
I have some very simple code to generate an assembly and invoke a method
Let's say you have a view called View Story which is just a web
I would like to know how I could generate code from a text file
I want to generate the hash code from a string,e.g.咖啡,but the hashcode I get
I am using Python multiprocessing to generate a temporary output file per process. They
The following code generates the primaey key for the new record to be inserted
Possible Duplicate: Weird “406 not acceptable” error following code generate a 406 Not Acceptable

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.