Python Forum

Full Version: USE string data as a variable NAME
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am kinda a beginner in python..
You might say that I have not been 'googling' for this question..
But..
I write code
for atr in dir(attributes['pDevMode']):
	print(atr, ': ',attributes['pDevMode'].atr)
I get error
Traceback (most recent call last):
  File "C:\Users\rokas\Desktop\Codin\Automatic printer selector\test.py", line 16, in <module>
    print(atr, ': ',attributes['pDevMode'].atr)
AttributeError: 'PyDEVMODEW' object has no attribute 'atr'
What I think I have found on internet so far
They just use value of a variable as a name for another variable and assign to it some meaning.
What I want to do
I do not want to assign another meaning to that value of a string, I want to use value of a string as a NAME for variable. For example if:
atr = 'PaperSize'
, I want that my line would correspond to
print(atr, ': ',attributes['pDevMode'].PaperSize)
Sincerely,
Rokas
Use the built-in function getattr() to get an attribute by name.

getattr(attributes['pDevMode'], atr)
https://docs.python.org/3/library/functions.html#getattr

While there, read about all the other built-in functions.