471,356 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

Output a variable name

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
5 1643
ilikepython
844 Expert 512MB
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
2,851 Expert Mod 2GB
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
844 Expert 512MB
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
6,596 Expert 4TB
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
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.

Similar topics

1 post views Thread by Duncan Bloem | last post: by
1 post views Thread by thomas | last post: by
3 posts views Thread by Sandman | last post: by
3 posts views Thread by super.raddish | last post: by
reply views Thread by Random | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.