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

  • Home
  • SEARCH
  • 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 6655423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:31:04+00:00 2026-05-26T01:31:04+00:00

I want to dynamically create classes at runtime in python. For example, I want

  • 0

I want to dynamically create classes at runtime in python.

For example, I want to replicate the code below:

>>> class RefObj(object):
...     def __init__(self, ParentClassName):
...         print "Created RefObj with ties to %s" % ParentClassName
... class Foo1(object):
...     ref_obj = RefObj("Foo1")
... class Foo2(object):
...     ref_obj = RefObj("Foo2")
... 
Created RefObj with ties to Foo1
Created RefObj with ties to Foo2
>>>

… but I want the Foo1, Foo2, Foo classes to be created dynamically (ie: during execution instead of on first-pass compile).

One way to achieve this is with type(), like so:

>>> class RefObj(object):
...     def __init__(self, ParentClassName):
...         print "Created RefObj with ties to %s" % ParentClassName
... def make_foo_class(index):
...     name = "Foo%s" % index
...     return type(name, (object, ), dict(ref_obj = RefObj(name)))
... 
>>> Foo1 = make_foo_class(1)
Created RefObj with ties to Foo1
>>> Foo2 = make_foo_class(2)
Created RefObj with ties to Foo2
>>> type(Foo1()), type(Foo2())
(<class 'Foo1'>, <class 'Foo2'>)

I can also achieve it with exec, like so:

>>> class RefObj(object):
...     def __init__(self, ParentClassName):
...         print "Created RefObj with ties to %s" % ParentClassName
... def make_foo_object(index):
...     class_template = """class Foo%(index)d(object):
...         ref_obj = RefObj("Foo%(index)d")
...         """ % dict(index = index)
...     global RefObj
...     namespace = dict(RefObj = RefObj)
...     exec class_template in namespace
...     return namespace["Foo%d" % index]
... 
>>> Foo1 = make_foo_object(1)
Created RefObj with ties to Foo1
>>> Foo2 = make_foo_object(2)
Created RefObj with ties to Foo2
>>> type(Foo1()), type(Foo2())
(<class 'Foo1'>, <class 'Foo2'>)

The use of exec doesn’t sit well with me (as I expect it doesn’t with a lot of people who read this question) but exec is exactly how python’s collections.namedtuple() class is implemented (see this line). Also very relevant is the defense of this use of exec here, by the creator of the class (Raymond Hettinger). In this defense, it is stated that “It is a key feature for named tuples that they are exactly equivalent to a hand-written class“, which one might take to imply that the use of type() is not as good as using exec…

Is there a difference? Why use exec vs type()?

I expect the answer may be that both ways are the same and it is simply that the namedtuple implementation has a lot of namedtuple variables peppered through it, and doing this with dynamically generate closures for all methods made the code get unwieldy, but I want to know if there is something more to this.

Regarding my discomfort with exec, I do recognize that if there is no way whatsoever for untrusted parties to inject nefarious code into it, it should be fine… it is just ensuring that that makes me nervous.

  • 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-26T01:31:05+00:00Added an answer on May 26, 2026 at 1:31 am

    There is no disadvantage to using type() over exec. I think Raymond’s defense is a bit defensive. You have to choose the technique that you find most readable and understandable. Both ways create confusing code.

    You should try really hard to avoid code that creates classes in the first place, that would be best.

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

Sidebar

Related Questions

I want to use JavaCompiler to dynamically create some classes. I found the source
I want to dynamically create object of HtmlDivElement in my jsf managed bean and
I have the array example below that I am using to dynamically create an
What is the best way to dynamically create a Python object instance when all
I want to dynamically create a class in c#.net. I tried using anonymous methods
In my code, I generate new python classes at runtime. For some of them,
I want to dynamically create controls in my bean. I am using JSF 2.0
Basically i want to be able to dynamically create a temp table based off
Is there a way to dynamically create a selectItem list? I dont really want
I want to create a file on the webserver dynamically in PHP. First I

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.