How do you print variable names in python?
Say I have a variable named Show
for use in making a dictionary. There's a good way to do this and I'm just missing it. EDIT: The reason to do this is thus. I am running some data analysis stuff where I call the program with multiple parameters that I would like to tweak, or not tweak, at runtime. I read in the parameters I used in the last run from a .config file formated as
When prompted for values, the previously used is displayed and an empty string input will use the previously used value. My question comes about because when it comes to writing the dictionary that these values have been scanned into. If a parameter is needed I run I think I will avoid the problem all together by reading the .
Constantin 26.9k10 gold badges59 silver badges79 bronze badges asked Feb 26, 2009 at 22:26
physicsmichaelphysicsmichael 4,60311 gold badges34 silver badges54 bronze badges 9 If you insist, here is some horrible inspect-based solution.
I hope it will inspire you to reevaluate the problem you have and look for another approach. answered Feb 26, 2009 at 23:05
5 To answer your original question:
Example:
As @rbright already pointed out whatever you do there are probably better ways to do it.
answered Feb 26, 2009 at 23:14
jfsjfs 382k179 gold badges944 silver badges1614 bronze badges 9 If you are trying to do this, it means you are doing something wrong. Consider using a
Output:
answered Feb 26, 2009 at 22:52
recursiverecursive 81.9k32 gold badges147 silver badges236 bronze badges 3 You can't, as there are no variables in Python but only names. For example:
Which of those two is now the correct variable? There's no difference between There's been a similar question before.
answered Feb 26, 2009 at 22:31
Georg SchöllyGeorg Schölly 122k48 gold badges211 silver badges264 bronze badges 7 Rather than ask for details to a specific solution, I recommend describing the problem you face; I think you'll get better answers. I say this since there's almost certainly a better way to do whatever it is you're trying to do. Accessing variable names in this way is not commonly needed to solve problems in any language. That said, all of your variable names are already in dictionaries which are accessible through the built-in functions locals and globals. Use the correct one for the scope you are inspecting. One of the few common idioms for inspecting these dictionaries is for easy string interpolation:
This sort of thing tends to be a bit more readable than the alternatives even though it requires inspecting variables by name. answered Feb 26, 2009 at 22:49
Ryan BrightRyan Bright 3,45721 silver badges19 bronze badges 2 With eager evaluation, variables essentially turn into their values any time you look at them (to paraphrase). That said, Python does have built-in namespaces. For example, locals() will return a dictionary mapping a function's variables' names to their values, and globals() does the same for a module. Thus:
Note that you don't need to import anything to be able to access locals() and globals(). Also, if there are multiple aliases for a value, iterating through a namespace only finds the first one. answered Feb 26, 2009 at 22:45
NikhilNikhil 5,6351 gold badge31 silver badges30 bronze badges 2 Will something like this work for you?
And in your example:
answered Feb 26, 2009 at 23:01
myroslavmyroslav 3,64322 silver badges28 bronze badges For the revised question of how to read in configuration parameters, I'd strongly recommend saving yourself some time and effort and use ConfigParser or (my preferred tool) ConfigObj. They can do everything you need, they're easy to use, and someone else has already worried about how to get them to work properly! answered Feb 26, 2009 at 23:39
James BradyJames Brady 26.2k7 gold badges50 silver badges57 bronze badges How do you write variable names in Python?Rules for Python variables:. A variable name must start with a letter or the underscore character.. A variable name cannot start with a number.. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). Variable names are case-sensitive (age, Age and AGE are three different variables). How do you check variable names in Python?python-varname is not only able to detect the variable name from an assignment, but also: Retrieve variable names directly, using nameof. Detect next immediate attribute name, using will. Fetch argument names/sources passed to a function using argname.
How do you print variables and strings in Python?Python Print Variable. Use the print Statement to Print a Variable in Python.. Use a Comma , to Separate the Variables and Print Them.. Use the String Formatting With the Help of %. Use the String Formatting With the Help of {}. Use the + Operator to Separate Variables in the print Statement.. How do you print a variable inside a function in Python?Print variable in python using 4 different methods. Method 1: Using comma , character.. Method 2: Using Format String.. Method 3: Using Format String with Positional Argument.. Method 4: Using plus + character.. |