473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1815
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

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

Similar topics

1
by: Duncan Bloem | last post by:
Hello, I am not a stylesheet designer, but I am wondering if (and how) the following XML file can be transformed to a custom defined text output file. from XML file: <?xml version="1.0"...
5
by: Marcel Akkerman | last post by:
Hi, Does anyone have a clue how to reduce the number of nodes using XSLT? When outputing all nodes in order I could just use <xsl:for-each select="name"> But what if I, besides sorting and...
3
by: Mike Conmackie | last post by:
Greetings, I am trying to create a node in the output tree using a variable. Here are some fragments that I hope will explain the problem better. <xsl:stylesheet...
1
by: thomas | last post by:
I'm building a guitar website which uses XML and XSLT. http://www.madtim67.com/guitar/index.html You can search either by artist or song. I used the <xsl:if test="contains(artist ,$text1)"> line...
1
by: SV | last post by:
I have two input XML files. One lists all the expected elements (default.xml) with default values and the other is the actual data (data.xml). For any elements that are missing in data.xml, I want...
3
by: Sandman | last post by:
Hello, I'm building a website in PHP and Javascript. The registration portion is divided into 2 sections: 1. In one, I get info about the visitor. This is sent via POST to a php script which is...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
0
by: Random | last post by:
I'm using xslt to parse data into a comma-delimted format. For a reason I can't fathom, the output is coming up with all the data for each row in a single column. The commas are there, but the...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
109
by: bonneylake | last post by:
Hey Everyone, Well i am having a problem outputting for my report and hoping someone can explain what i am doing wrong. What the report is about is comparing my company's part price's to d and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.