-5

I have a variable fruit = 13, when I print(fruit) I get 13.

How do I print out the variable name so I get 'fruit' to print out? I don't want to have to use print('fruit') because I will be using functions.

ZdaR
  • 22,343
  • 7
  • 66
  • 87
biglemon29
  • 35
  • 7
  • 1
    Why would you want to do this? – Kevin Jun 08 '15 at 17:49
  • So if I print out variables the people reading it will know what the numbers correspond to. – biglemon29 Jun 08 '15 at 17:52
  • Your last sentence does not follow. Using functions just means you pass a variable to the function, and print it out using the name it's assigned to by the function declaration. – Daniel Roseman Jun 08 '15 at 17:52
  • I don't understand what you mean also I am new to python – biglemon29 Jun 08 '15 at 17:54
  • 1
    Looks very similar to http://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string-in-python – Bhargav Rao Jun 08 '15 at 17:54
  • If I have a variable that equals a number I want to print the name of the variable – biglemon29 Jun 08 '15 at 17:54
  • Stop repeating that and explain **why**. Names in Python are really just labels for underlying objects (see e.g. http://nedbatchelder.com/text/names.html); there is likely to be a much easier and more sensible way to achieve... whatever it is you want. – jonrsharpe Jun 08 '15 at 17:59
  • why not just this? `print [name for name in globals() if globals()[name] is fruit]` – John Ruddell Jun 08 '15 at 18:04
  • 1
    If you can find it in your heart not to downvote, please do. It's not as obvious to new programmers as it is to us why this is a "bad" question. And downvoting isn't going to teach anything. – Eli Rose Jun 08 '15 at 18:09
  • biglemon29 what @EliRose is trying to say is on Stack Overflow a question that has been asked multiple times already should not be asked again. a simple google search returns multiple ways to solve your issue. the reason why people are downvoting is probably because A. it is a duplicate question. or B. your question appears to lack any research. Next time you ask a question just make sure you have tried to find a way to do it and also try and post any attempts you have made to help the community understand where you are coming from :) – John Ruddell Jun 08 '15 at 18:24

5 Answers5

2

What you're looking for is a dictionary.

food = {'fruit':13, 'vegetable':15}

for item in food:
    print(item, food[item])

Result:

fruit 13
vegetable 15
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

Just print the next key in the locals dictionary with the desired value. Below 13 is the value:

print next(k for k, v in locals().items() if v == 13) # get variable that is equal to 13
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

You could achieve a similar behaviour by using a dictionary and a for-loop iterating through it:

#!/usr/bin/env python3
# coding: utf-8

# define a dict with sample data
d = {'fruit': 13}

# print keys and values of sample dict
for key, val in d.items():
    print(key, val)

Output looks like:

fruit 13
albert
  • 8,027
  • 10
  • 48
  • 84
0

you could just do this.

print [n for n in globals() if globals()[n] is fruit][0]

>>> fruit = 13
>>> n = None
>>> print [n for n in globals() if globals()[n] is fruit][0]
fruit
>>> b = None
>>> b = [n for n in globals() if globals()[n] is fruit][0]
>>> b
'fruit'
>>> type(b)
<type 'str'>
>>>

you can also make an object off of a variable in your namespace like this if you'd like

b = {globals()[n]: n for n in globals() if globals()[n] is fruit}

which you can then go and get the name text out by the value of that object

print b[fruit]

output:

>>> b = {globals()[n]: n for n in globals() if globals()[n] is fruit}
>>> b
{13: 'fruit'}
>>> fruit
13
>>> b[fruit]
'fruit'
John Ruddell
  • 25,283
  • 6
  • 57
  • 86
0

While there are ways to do this, (personally I think a dictionary is what you want) I'd like to explain why many people are balking at it, since on the face of it, it seems like a pretty reasonable task.

But there's a reason this is so difficult in Python (and most other languages I can think of). The reason is that, most of the time, one can think of a variable name as shorthand for a real, underlying meaning which is in your head. Most languages are designed with the idea that changing the shorthand should not change the output of the code.

Suppose you have code like this:

fruit = 10
print('Amount of fruit', fruit)

There are a million names we could have chosen for fruit -- fruit_quantity, num_fruits, etc. but one underlying concept that variable represents, which is: amount of fruit. So if you want a label for the number, use the name of the underlying concept.

Eli Rose
  • 6,788
  • 8
  • 35
  • 55