By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

Output a variable name

P: 51
Is there any way to output a variable name, without knowing the variable in advance. (Ex. Myvar = 0, could i output "Myvar" )?
Jun 11 '07 #1
Share this Question
Share on Google+
5 Replies


ilikepython
Expert 100+
P: 844
Is there any way to output a variable name, without knowing the variable in advance. (Ex. Myvar = 0, could i output "Myvar" )?
I don't really understand what you're asking. Could you please clarify?

Thanks
Jun 11 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Is there any way to output a variable name, without knowing the variable in advance. (Ex. Myvar = 0, could i output "Myvar" )?
You would have to have some means to identify the string for output (eg keep the data stored in a dictionary where the variable names are the dictionary keys). The global and local dictionaries contain these variables, so you may be able to access them that way.
Expand|Select|Wrap|Line Numbers
  1. >>> def var_name():
  2. ...     xyz = 0
  3. ...     for item in locals():
  4. ...         print item
  5. ...         
  6. >>> var_name()
  7. xyz
  8. >>> 
  9. >>> def var_names():
  10. ...     abc = 12
  11. ...     xyz = 13
  12. ...     for item in locals():
  13. ...         yield item
  14. ...         
  15. >>> for i in var_names():
  16. ...     print i
  17. ...     
  18. xyz
  19. abc
  20. >>> 
Jun 11 '07 #3

ilikepython
Expert 100+
P: 844
You would have to have some means to identify the string for output (eg keep the data stored in a dictionary where the variable names are the dictionary keys). The global and local dictionaries contain these variables, so you may be able to access them that way.
Expand|Select|Wrap|Line Numbers
  1. >>> def var_name():
  2. ...     xyz = 0
  3. ...     for item in locals():
  4. ...         print item
  5. ...         
  6. >>> var_name()
  7. xyz
  8. >>> 
  9. >>> def var_names():
  10. ...     abc = 12
  11. ...     xyz = 13
  12. ...     for item in locals():
  13. ...         yield item
  14. ...         
  15. >>> for i in var_names():
  16. ...     print i
  17. ...     
  18. xyz
  19. abc
  20. >>> 
Ah, now I know :)
Jun 11 '07 #4

bartonc
Expert 5K+
P: 6,596
Is there any way to output a variable name, without knowing the variable in advance. (Ex. Myvar = 0, could i output "Myvar" )?
I always use the inspect module:
Expand|Select|Wrap|Line Numbers
  1. >>> import inspect
  2. >>> class aClass:
  3. ...     aValue = 1
  4. ...     bValue = 'test'
  5. ...     
  6. >>> anInstance = aClass()
  7. >>> print [(name, value) for name, value in inspect.getmembers(anInstance) if not name.startswith("_")]
  8. [('aValue', 1), ('bValue', 'test')]
  9. >>>
Jun 11 '07 #5

P: 51
Thanks bvdet i understand now.
Jun 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.