1

It might be a silly thing to do in practice, but is it possible to do something like:

from data import myvariables

class Example():
    def __init__(self):
        self.* = myvariables.*

instead of doing:

from data import myvariables

class Example():
    def __init__(self):
        self.variable1 = myvariables.variable1
        self.variable2 = myvariables.variable2
        ...

myvariables is just a file that contains a bunch of variables like variable1 = 1; variable2 = 'string', etc.

ru111
  • 813
  • 3
  • 13
  • 27
  • 2
    I'd say that, if they come from anther module, they are not specific to a instance and shouldn't be attributs of the class, just use them when needed – azro Apr 03 '20 at 10:10
  • That makes sense. I was toying with this idea as a temporary workaround for something, and was curious if it was even possible. – ru111 Apr 03 '20 at 10:13
  • 2
    If you need this kind of behaviour (why I wouldn't even start got guess) and have full control over both classes: export a dictionary of the modules variables and import them into your other class. You are probably waaaay out of sensible land if you think you need to do this. Read about `**kwargs` how to do that: https://stackoverflow.com/questions/1769403/what-is-the-purpose-and-use-of-kwargs – Patrick Artner Apr 03 '20 at 10:13
  • That's a much better idea, thanks. Yeah I realise it is quite redundant – ru111 Apr 03 '20 at 10:17
  • @PatrickArtner after a quick look it seems quite complicated - I have to convert all the variable names in the file into a list of strings (not easy) and it's not mutable in the same way the code works so I have to convert them back to variables after importing it. I'll think about another workaround... – ru111 Apr 03 '20 at 10:32

0 Answers0