Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 15th, 2007, 2:49 AM   #1
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
string to variable... or something

say I have a variable called example I want to print the contents of example, but I want to print it using its name from a string. Like so:

print "example"

naturally it will print the string example and not the contents of the variable example. How do I... de-string a string if you will?
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!"
MR.T is offline   Reply With Quote
Old Jun 15th, 2007, 3:42 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
I've never used Python in my life, but what you want might be possible if the language has some mechanism for determining attributes at run time, similar to System.Runtime.Reflection in C#.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jun 15th, 2007, 8:52 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
How about using string concatenation via + ?

#!/usr/bin/python

name = "Jason";
print "Hello " + name + ". How are you?";
Execution:

[jpowers@pandora ~]$ python string.py
Hello Jason. How are you?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jun 15th, 2007, 8:59 AM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Something like this maybe?

class Person(object):
	def __init__(self, name, age):
		self.name = name
		self.age = age

def main():
	p = Person("Joe Smith", 38)
	attr = raw_input("Enter an attribute of a person: ")
	print "The person's %s is  %s." % (attr, getattr(p, attr))
	
if __name__ == "__main__":
	main()


EDIT: Ah, I think I misunderstood your question.
Too print the contents of a variable in a string, just use the "%s" specifier and follow it with an argument.

For example:
name = "Joe"
print "Hello, %s!" % name
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jun 15th, 2007, 8:59 AM   #5
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
In Python, there are two types of variables: locals and globals. Locals are variables defined inside functions/methods without the 'global' keyword, ones at module level are global.

Just some code now (with some other introspection to boot):

>>> a = 1
>>> def f():
...   b = 2
...   print locals()
...   print globals()
...   print locals()['b']
... 
>>> f()
{'b': 2}
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, 'a': 1, 'f': <function f at 0x2abb77914758>}
2
>>> class C:
...   def __init__(self):
...     print 'initializing nothing in particular'
...   def doNothing(self):
...     pass
... 
>>> dir(C)
['__doc__', '__init__', '__module__', 'doNothing']
>>> hasattr(C, 'doNothing')
True
>>> i = C()
initializing nothing in particular
>>> getattr(i, 'doNothing')
<bound method C.doNothing of <__main__.C instance at 0x2abb7791c9e0>>
>>> getattr(i, 'doNothing')()
free-zombie is offline   Reply With Quote
Old Jun 15th, 2007, 12:53 PM   #6
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
hmm, I I don't think I explained what I am trying to do very well. I guess what I am asking is kind of similar to unpickling. Let me try again to explain.

joe = "this is what I want to print"
bob = ["joe"]
print bob[0]

This would print the word "joe", but I want to actually print the value of joe which is "this is what I want to print". Is this possible?
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!"
MR.T is offline   Reply With Quote
Old Jun 15th, 2007, 2:01 PM   #7
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
I think I explained that in my code. You might want to read it carefully, especially lines 3, 4 and 6.
free-zombie is offline   Reply With Quote
Old Jun 15th, 2007, 2:23 PM   #8
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
Yes, free-zombie your post did explain what I was looking for.

I found out that there was a much simpler solution to the problem I was dealing with however. It seems like it is always like that with python and me, every time I do something I get hung up because I don't try the painfully simple and obvious things, I guess I just always expect it to be harder than it is.

Thanks for all the helpful replies guys.
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!"
MR.T is offline   Reply With Quote
Old Jun 28th, 2007, 12:10 PM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Talking

I assume you found the evil function eval():
joe = "this is what I want to print"
bob = ["joe"]
print eval(bob[0])  # this is what I want to print
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 1:01 PM
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 12:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC