![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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!" |
|
|
|
|
|
#2 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
How about using string concatenation via + ?
#!/usr/bin/python name = "Jason"; print "Hello " + name + ". How are you?"; [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." |
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#5 |
|
Programmer
|
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')() |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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!" |
|
|
|
|
|
#7 |
|
Programmer
|
I think I explained that in my code. You might want to read it carefully, especially lines 3, 4 and 6.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0
![]() |
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!" |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |