USE string data as a variable NAME - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: USE string data as a variable NAME (/thread-38337.html) |
USE string data as a variable NAME - rokorps - Sep-30-2022 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 RE: USE string data as a variable NAME - deanhystad - Sep-30-2022 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. |