I have been reading the replies and wonder sometimes if we understand the real question as intended.
Classes in Python can be changed in all kinds of ways even after they have been defined and the changes take effect on any new instances created afterward. So can instances in multiple ways. If you want to store the names of a hundred columns in a variable or even a hundred variables, you have ways to assign them. You can even change methods on the fly. If what you want is even more flexibility to design the class later after receiving more data such as the names and types of the columns in a data table, you can either write the description as text into a temporary file and import it, if that makes sense, or make a string to be evaluated in memory. Both can be dangerous if you do not trust the parts added as the code is going to be run at runtime and can do malicious things. Python often has so many ways to do things that various ones may work better for you. In your case, one example would be to intercept the ability to set and get (unknown) components of a class or instance by using the right dunder function such as __getattr__ and have it KNOW about your dynamic variable names and control access to them. There are many ways to do this, CAREFULLY, and some work only or differently in new style classes. Heck, you can put all the important code in an external function called by the above that can dynamically be made in Python at a later time. One architecture might be to store your new info in one or more dictionaries and have that functionality check if a valid request is made and return it. Obviously it matters where you want the data held as in per instance or per class or superclass and so on. Of course, I may misunderstand your issue. But from what it sounds like, your main request is a way to associate multiple items to be stored after a class is created but before it is used. There are an amazing number of ways even before you loom at more advanced methods like decorators. -----Original Message----- From: Tutor <tutor-bounces+avigross=verizon....@python.org> On Behalf Of Oscar Benjamin Sent: Wednesday, November 7, 2018 5:33 PM To: tutor@python.org Subject: Re: [Tutor] best way to dynamically set class variables? On Wed, 7 Nov 2018 at 18:35, Alan Gauld via Tutor <tutor@python.org> wrote: > > On 07/11/2018 14:48, Albert-Jan Roskam wrote: > > > What is the best way to dynamically set class variables? > > I think I'm maybe missing the point of your question? I think you are as well :) IIUC then the question is: how can I programatically/dynamically create a class that has some attributes derived from data that is known at runtime? Am I understanding this correctly Albert? > > # ------- > > class Parent: pass > > class_vars = dict(col1='str', col2='int') > > > > # approach 1 > > Child = type('Child', (Parent,), class_vars) This seems fine to me. It may seem cryptic but that's only because it's unusual to do this. You are creating a "type" and that is the constructor for type objects. -- Oscar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor