Why it is not possible to import a variable from a module by using string operators?

88 views Asked by At

I have a list for variables of fruits in a python script:

VariableScript.py:

variableColorApple = "Red"
variableWeightApple = ["5", "6", "7"]
variablePriceApple = ["10", "15", "20"]
variableColorApple = "Orange"
variableWeightOrange =["8", "9", "10"]
variablePriceOrange =["10", "15", "20"]
#...

And I have another script, where I use user input (user's fruit choice), to run my analysis. I want to write only one script that I can runregardless of the user's fruit selection. So I could avoid if else statements and long scripts.

I thought I could use string operators to import variables, but with string operators python doesn't find the variable in VariableScript module. What else I can try?

I have also already tried operator.getattr(). But then I couldnt access the items in the gettattr().

Just to keep it simple and executable: Let's say the users' input is "Apple" and I just want to print the color of apple from VariablesScript.py. My code is:

from VariableScript import VariablesScript as Variables

userInput = "Apple"
print(Variables.variableColor + UserInput)`

Any idea how can I get the color or apple without actually writing apple?...

2

There are 2 answers

0
nigh_anxiety On BEST ANSWER

So what you're asking to do is technically possible, but it is definitely un-Pythonic and really not a stable design.

Here's how to get your code to work:

from VariableScript import VariablesScript as Variables

userInput = "Apple"
try:
    var = "variableColor" + userInput
    value = getattr(Variables, var)
except AttributeError as e:
    print(f"The variable '{var}' is not defined.")
else:
    print(value)

A much better solution here would be to use a data structure such as a dictionary, which has string key lookup.

#variables.py

fruits = {
    "apple" : {
       "color": "Red",
       "weight": ["5", "6", "7"],
       "price" : ["10", "15", "20"]
       },
    "orange" : {
        "color": "Orange",
        "weight": ["8", "9", "10"],
        "price" : ["10", "15", "20"]
        }
}


#main.py

from variables import fruits

user_input = "Apple"

fruit_data = fruits.get(user_input.lower())
if fruit_data is None:
    fruit_folor = f"{user_input} was not found"
else:
    fruit_color = fruit_data["color"]

print(fruit_color)

Edit: added safeguard for case where fruits.get(user_input.lower()) returns None and fixed my brackets.

You may also want to look into DataClasses or NamedTuples as a data structure to use for each fruit. Also, if the weights and prices lists are meant to be in sync, then perhaps they should be a in a dictionary with weight as the key, and price as the value, or it should be a single list of tuples.

2
Agan On

Use exec() function to execute a string as Python code.

from VariableScript import VariablesScript as Variables

userInput = "Apple"
variableColor = 'variableColor' + userInput
exec(f'color = Variables.{variableColor}')
print(color)

or use a dictionary to store the variable names and their corresponding values:

from VariableScript import VariablesScript as Variables

fruit_data = {
    'Apple': {'color': Variables.variableColorApple, 'weight': Variables.variableWeightApple, 'price': Variables.variablePriceApple},
    'Orange': {'color': Variables.variableColorOrange, 'weight': Variables.variableWeightOrange, 'price': Variables.variablePriceOrange},
}

user_input = 'Apple'
color = fruit_data[user_input]['color']
print(color)