Global dict name in a variable
Question:
I want to have more dict’s in my init.py and I want to set its name in a variable. But it wouldn’t detect it as the name.
My Program:
from StackOverflow import *
number = input("Which car do you want:")
car = r"Car"+number
print(car["Color"])
print(car["Brand"])
StackOverflowinit.py:
Car1 = {
"Color": "Blue",
"Brand": "Aston Martin"
}
Car2 = {
"Color": "Red",
"Brand": "Volvo"
}
I expect that it give the color and brand from the chosen car.
But I get this error:
Traceback (most recent call last):
File "D:/Users/stanw/Documents/Projecten/Stani Bot/Programma's/StackOverflow/Choose Car.py", line 5, in <module>
print(car["Color"])
TypeError: string indices must be integers
Answers:
try to change :
car = r"Car"+number
to:
car = globals()["Car" + number]
or:
car = eval("Car"+number)
approach with global variable only works if the variable is in the current module. To get a value in another module, you can use getattr:
import other
print getattr(other, "name_of_variable")
getattr(object, name[, default])
Return the value of the named attribute of object. name must be a
string. If the string is the name of one of the object’s attributes,
the result is the value of that attribute. For example, getattr(x,
‘foobar’) is equivalent to x.foobar. If the named attribute does not
exist, default is returned if provided, otherwise AttributeError is
raised.
something like :
import StackOverflow
number = input("Wich car do you want:")
car = r"Car"+number
print (getattr(StackOverflow, car))
A note about the various “eval” solutions: you should be careful with eval, especially if the string you’re evaluating comes from a potentially untrusted source — otherwise, you might end up deleting the entire contents of your disk or something like that if you’re given a malicious string.
Since Python 3.7, you can use getattr
on modules.
import StackOverflow
number = input('Enter a number')
var_name = f'Car{number}'
if hasattr(StackOverflow, var_name):
car = getattr(StackOverflow, var_name)
else:
print('Car not found')
I want to have more dict’s in my init.py and I want to set its name in a variable. But it wouldn’t detect it as the name.
My Program:
from StackOverflow import *
number = input("Which car do you want:")
car = r"Car"+number
print(car["Color"])
print(car["Brand"])
StackOverflowinit.py:
Car1 = {
"Color": "Blue",
"Brand": "Aston Martin"
}
Car2 = {
"Color": "Red",
"Brand": "Volvo"
}
I expect that it give the color and brand from the chosen car.
But I get this error:
Traceback (most recent call last):
File "D:/Users/stanw/Documents/Projecten/Stani Bot/Programma's/StackOverflow/Choose Car.py", line 5, in <module>
print(car["Color"])
TypeError: string indices must be integers
try to change :
car = r"Car"+number
to:
car = globals()["Car" + number]
or:
car = eval("Car"+number)
approach with global variable only works if the variable is in the current module. To get a value in another module, you can use getattr:
import other
print getattr(other, "name_of_variable")
getattr(object, name[, default])
Return the value of the named attribute of object. name must be a
string. If the string is the name of one of the object’s attributes,
the result is the value of that attribute. For example, getattr(x,
‘foobar’) is equivalent to x.foobar. If the named attribute does not
exist, default is returned if provided, otherwise AttributeError is
raised.
something like :
import StackOverflow
number = input("Wich car do you want:")
car = r"Car"+number
print (getattr(StackOverflow, car))
A note about the various “eval” solutions: you should be careful with eval, especially if the string you’re evaluating comes from a potentially untrusted source — otherwise, you might end up deleting the entire contents of your disk or something like that if you’re given a malicious string.
Since Python 3.7, you can use getattr
on modules.
import StackOverflow
number = input('Enter a number')
var_name = f'Car{number}'
if hasattr(StackOverflow, var_name):
car = getattr(StackOverflow, var_name)
else:
print('Car not found')