473,396 Members | 1,972 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,396 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 1814
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.