0

Here is my situation. I need to get a data package from a server and then copy data from that package to a local file. As I iterate through the local file, I need to be able to pull the correct data from the package.

My data package will look like this:

(DataObject){
  var1 = "foo"
  var2 = "bar"
  var3 = True
}

I have a dictionary set up that can look at the local file and determine the name of the variable I need. However, I need to be able to access that variable from the object in a format like this serverResult.result.[variableName]

I looked around on StackOverflow as well as some other places, and I found that Python supports eval, but after reading more, I came to the (smart) conclusion that I needed another solution.

One solution I thought of was to iterate through the data object and create a dictionary of its member variables. However, this is not my first choice because I need to access potentially hundreds of data objects, and they're longer than the simplified version I demonstrated above, so I wanted to see if there was a more efficient solution than creating a new dictionary for each data object I need to access.

I can provide more information to help get an answer if needed.

I cannot change how I get the data. The API I am using is limited to just the way I described above.

Thanks!

1 Answers1

0

Credit to mkrieger1 for this answer

getattr(object, "variableName")

For the example in the question: getattr(serverResult.result, "var1")