In my case, I'd like to save and restore some "plain" variables (i.e., ints, strings) in a file, which would end up as class properties. This example is the closest I got, by using import:
a.py
b = 134
a = "hello"
mytest.py
import inspect
class Teost:
from a import *
def __init__(self):
self.c = 12
print(inspect.getmembers(self)) # has a and b
print(self.__dict__) # no a and b
print(self.a) # prints "hello"
xx = Teost()
So, here a.py acts as the file storing the variable values (a and b), and from a import * inside the class gets them as class properties (self.a and self.b), which is pretty much what I want.
Unfortunately, turns out using starred import syntax in a class is frowned upon:
$ python mytest.py
mytest.py:3: SyntaxWarning: import * only allowed at module level
class Teost:
[('__doc__', None), ('__init__', <bound method Teost.__init__ of <__main__.Teost instance at 0x7fdca368ab90>>), ('__module__', '__main__'), ('a', 'hello'), ('b', 134), ('c', 12)]
{'c': 12}
hello
... so I get an ugly "SyntaxWarning: import * only allowed at module level", which I cannot get rid of (unless I disable warnings, which I don't want to do)
So, do I have any other options, to use a file written as a.py (i.e. in plain-text, Python syntax), and have the variables in it end up as some classes properties?
(I've seen How do I save and restore multiple variables in python?, but I'm not interested in pickle or shelve because neither of them writes in a Python-syntax, plain-text file)