How to initialize object attributes from an array of strings in Python?

292 Views Asked by At

I am supposed to write a class which will allow the user of the script to define what attributes will it's objects have when initializing them by giving it an array of strings like this.

data = Database(tables=['distance', 'speed'])

Then it should be possible to call the class's methods like

data.distance.insert({1: 25, 2: 55})
data.speed.mean()

etc.

I have tried using setattr() this way

data = Database()
tables=['distance', 'speed']
for item in tables:
    setattr(data, item, item)

which works, but isn't exactly what it should be.

Any ideas how to do it directly inside the class?

0

There are 0 best solutions below