I'm kind of a newbie in Python and I'd like to know if the following is possible. All of the following happens at run-time. The application's presentation is basically a Python interpreter like the IDLE, where the user types Python code, hits enter and sees the results immediately. The difference is that this interpreter can generate new types on the fly, given a type definition is some arbitrary format (xml for instance). When it receives that type definition, it generates a new python class based on that type definition, with the same methods and fields etc. It also generates some extra code in the methods of that new type. The new type is then available to the user on the console so he can instantiate it and use it.
MyType is now a python class loaded in the interpreter.
User types "mt = MyType()", uses mt's fields, methods, etc.
So basically this requires some metaprogramming ability and I wonder if that's possible in Python? If so, using what libraries/types? I've browsed the documentation and I'm not really sure. I understand that I can create a new run-time type using the function type, but I feel like that's insufficient given I also need to be able to plug some more code in there, and all that has to happen automatically, the user just specifies where to find the type definition and it should be available to him.
Question
Andre S. Veteran
I'm kind of a newbie in Python and I'd like to know if the following is possible. All of the following happens at run-time. The application's presentation is basically a Python interpreter like the IDLE, where the user types Python code, hits enter and sees the results immediately. The difference is that this interpreter can generate new types on the fly, given a type definition is some arbitrary format (xml for instance). When it receives that type definition, it generates a new python class based on that type definition, with the same methods and fields etc. It also generates some extra code in the methods of that new type. The new type is then available to the user on the console so he can instantiate it and use it.
Basic use case :
User types "load type "MyType.xml"
MyType.xml :
MyType is now a python class loaded in the interpreter.
User types "mt = MyType()", uses mt's fields, methods, etc.
So basically this requires some metaprogramming ability and I wonder if that's possible in Python? If so, using what libraries/types? I've browsed the documentation and I'm not really sure. I understand that I can create a new run-time type using the function type, but I feel like that's insufficient given I also need to be able to plug some more code in there, and all that has to happen automatically, the user just specifies where to find the type definition and it should be available to him.
Thanks for any help.
Link to comment
Share on other sites
2 answers to this question
Recommended Posts