-2

Im wondering if there is a way to take a variable's name and convert it into a string to print it?

for example:

myVariable = 1

i want to convert "myVariable" like the name of the variable itself not the value of it into a string and then print that string to a console, is this possible?

ManxFox
  • 1
  • 1
  • 5
  • 1
    How about `print "myVariable"`? – Kevin May 12 '16 at 14:48
  • 1
    You can use the dictionary returned by `locals()`. Is that sufficient? Note that Python variables are references, so there could be many variables referring to the same single object. – cdarke May 12 '16 at 14:48
  • cdarke, how would I go about doing that? also, Kevin, sorry that would just print 1 – ManxFox May 12 '16 at 14:49
  • 4
    http://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string-in-python – Ian May 12 '16 at 14:49
  • Not certain what you need. For example, do you want all the variable names with the value 1? – cdarke May 12 '16 at 14:50
  • Values do not have any information about their names. You might be able to scrape it out from `locals()`, but why do you want to do this? – Jared Goguen May 12 '16 at 14:51
  • @Ian In particular, [this answer](http://stackoverflow.com/a/2553481/2247550). – Jared Goguen May 12 '16 at 14:52
  • 1
    Why do you need this? I know the question sounds dismissive, but if something is nearly impossible and it seems like you need it maybe you don't really understand what you need. Could you explain your use case a little bit? – Steven Rumbalski May 12 '16 at 14:52
  • @JaredGoguen ah yes, that answer - not the accepted. – Ian May 12 '16 at 14:53
  • If you do have a group of things that you want to hold on to the name of use a dictionary. – Steven Rumbalski May 12 '16 at 14:54
  • I can try, basically, I'm doing this for an inventory system I was using without the use of a dictionary (for other reasons I won't go into) the idea is there's 20 or so variables, one for each item and instead of using a giant if/elif statement to determine what is printed to the console I was hoping I could use a simple if statement and have it dynamically change depending on which item is being added to the inventory. – ManxFox May 12 '16 at 14:55
  • @ManxFox: An inventory system shouldn't require you to edit source code or care about variable names every time you decide to carry a new product. This problem requires an extra layer of indirection, which is provided by a dictionary or database. – Steven Rumbalski May 12 '16 at 14:57
  • 1
    @ManxFox It seems unlikely that the reasons for not using a dictionary are good reasons. A dictionary links names to values, exactly what you're trying to do here. The `locals()` solution teases out a dictionary from the current scope, effectively doing the same thing as making your own dictionary, but in a clumsy fashion. – Jared Goguen May 12 '16 at 14:57
  • Like I said, there are reasons I can't use a dictionary for this that I won't go into if I could use a dictionary it would be a lot more simple to get this system working, unfortunately the system I'm working on isn't able to use dictionaries as it would require me recoding the entire thing, and that is something I definately do not want to do – ManxFox May 12 '16 at 14:58
  • 1
    Rock meet hard place. You're going to need to recode. Don't continue to sink time into an impossible design. – Steven Rumbalski May 12 '16 at 15:04

2 Answers2

1

Just need to pick the right once from the dict that locals() gives you.

>>> a = 2
>>> b = 3
>>> 
>>> print(locals())
{'__doc__': None, 'b': 3, 'a': 2, '__spec__': None, 
'__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', 
'__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__package__': None}

Probably better to put the variables you want to print into their own dict and then print the dict keys

>>> myvars = {'a': 2, 'b': 3}
>>> print(', '.join(myvars))
a, b

Or, with locals() again

>>> a = 2
>>> b = 3
>>> 
>>> print([x for x in locals() if not x.startswith('_')])
['b', 'a']
C14L
  • 12,153
  • 4
  • 39
  • 52
  • this wouldnt work for what I'm wanting it for, as the printed variable is actually not going to be the same variable all the time, it will change between 20 or so other variables – ManxFox May 12 '16 at 14:50
  • See the update, put them into their own dict so you know what you need to print. – C14L May 12 '16 at 14:52
  • can't use Dictionaries for what I need it for haha – ManxFox May 12 '16 at 14:57
  • Filter them out of the sea of `locals()`, see updated answer. – C14L May 12 '16 at 14:58
-2

It sounds like what you're looking for is quotation marks. Does this do what you need?

>>> myVariable = 1
>>> yourVariable = 2
>>> print ("The value of {} is {}".format("myVariable", myVariable))
The value of myVariable is 1
>>> print ("The value of {} is {}".format("yourVariable", yourVariable))
The value of yourVariable is 2
>>>
QuestionC
  • 10,006
  • 4
  • 26
  • 44