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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:44:21+00:00 2026-06-03T14:44:21+00:00

I’ve seen these Dynamically create a class questions which are answered saying, use the

  • 0

I’ve seen these “Dynamically create a class” questions which are answered saying, “use the type() function”. I’m sure I’ll have to at some point but right know I’m clueless. But from what I’ve seen you have to already know something about the class, such as a name.

What I’m trying to do is parse an idl type of file and from that create a class which will have methods and attributes. So I have NO knowledge up front of what the class name, functions, arguments or anything will be until I parse the string.

Any ideas?

  • 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-06-03T14:44:23+00:00Added an answer on June 3, 2026 at 2:44 pm

    http://docs.python.org/library/functions.html#type

    It’s a bit hard to Google for, but you can search for python type(name, bases, dict) function examples to get:

    http://www.voidspace.org.uk/python/articles/metaclasses.shtml

    An excerpt from the above, which gets to the heart of your question:


    The following are basically equivalent:

    def __init__(self, x):
        self.x = x
    
    def printX(self):
        print self.x
    
    Test = type('Test', (object,), {'__init__': __init__, 'printX': printX})
    

    and:

    class Test(object):
        def __init__(self, x):
            self.x = x
    
        def printX(self):
            print self.x
    

    There are two ways to create functions on the fly that I can think of. The usually-bad way is to write the code and reparse it (though done correctly, this can greatly increase performance). The sane way is to implement a function which interprets your IDL. This is called a higher-order function: http://effbot.org/pyfaq/how-do-you-make-a-higher-order-function-in-python.htm

    An example of what you would write, if you cannot find an interpreter for your IDL (of if it’s a custom IDL) is something like the link above, such as:

    def makeMethod(idlCode):
        syntax = MyIDL.parse(idlCode)
    
        def newMethod(*args, **kw):
            if syntax.statementType == MyIDL.IF_STATEMENT:
                if secureLookup(mySyntaxTree.IF):
                   return secureLookup(args[0]) 
                else:
                   return secureLookup(args[1])
            ...
    
        return (syntax.methodName, newMethod)
    

    There are many more elegant ways to expand this method, if you set up a mapping between constructs of your IDL and the syntax of *args and **kw, but this gives you the most flexibility and is the most straightforward and basic way I could think of.

    Then you’d do pass in:

    class DynamicIdlClass(object):
        ...
    
    for idlObject in idlCode:
        methods = dict(makeMethod(clause) for clause in idlObject.clauses})
        methods['__init__'] = makeInitMethod(idlObject.initClause)
        idlObject = type('Test', (DynamicIdlClass,), methods)
    
        yield idlObject  # or idlObjectsList.push(idlObject), etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.