0

I have a list that I would like to use information from in the naming of an Object. for example:

print this_device # ex. output ['dev879:', 'drain', '7.474', '11.163', '18.637']

What I would like to do it use this_device[0] to be the name of the next variable to use to create an object.

For example:

print this_device # ex. ['dev879:', 'drain', '7.474', '11.163', '18.637']

Drive_DeviceName_ = this_device[0] # which is 'dev879'

Drivedev879 = ReadableDevice(this_device[0], this_device[1], this_device[2])

Please let me know if this makes sense.

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
  • Notice that some of your names have `:` in them. This makes them illegal variable names. However, you should still be able to manipulate `globals()` to make this work, which I don't you really want to do. This smells like an XY problem to me – inspectorG4dget Nov 03 '14 at 00:59
  • 1
    Use a dictionary instead. – M. K. Hunter Nov 03 '14 at 01:05

2 Answers2

0

As far as I know that isn't possible. What you can do is to create a dictionary from the list and use the names you would like as keys.

devices = {}
devices['name'] = this_device[0]
etc...

This could be done faster by creating a list of the property names from your original list. I'm assuming that the list you provided is a list of properties.

props = ['name', 'prop_1', 'prop_2', ...]
this_device = ['dev879:', 'drain', '7.474', '11.163', '18.637']
device = dict(zip(props, this_device))

will result in:

{'name': 'dev870', 'prop_1': 'drain', ..}
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
0

If I understand, what you want to do, you can use globals:

globals()['b'] = 3
# Now there is a variable named b
print b
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187