Python Forum
USE string data as a variable NAME
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
USE string data as a variable NAME
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 306 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Replacing String Variable with a new String Name kevv11 2 793 Jul-29-2023, 12:03 PM
Last Post: snippsat
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,914 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Need help on how to include single quotes on data of variable string hani_hms 5 2,058 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,835 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,311 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,801 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,311 Jul-11-2022, 08:52 AM
Last Post: japo85
  Can you print a string variable to printer hammer 2 1,963 Apr-30-2022, 11:48 PM
Last Post: hammer
Question How to convert string to variable? chatguy 5 2,442 Apr-12-2022, 08:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020