Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   string to variable... or something (http://www.programmingforums.org/showthread.php?t=13351)

MR.T Jun 15th, 2007 2:49 AM

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?

lectricpharaoh Jun 15th, 2007 3:42 AM

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#.

Infinite Recursion Jun 15th, 2007 8:52 AM

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?

Jessehk Jun 15th, 2007 8:59 AM

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


free-zombie Jun 15th, 2007 8:59 AM

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')()


MR.T Jun 15th, 2007 12:53 PM

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?

free-zombie Jun 15th, 2007 2:01 PM

I think I explained that in my code. You might want to read it carefully, especially lines 3, 4 and 6.

MR.T Jun 15th, 2007 2:23 PM

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.

Dietrich Jun 28th, 2007 12:10 PM

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



All times are GMT -5. The time now is 6:00 PM.

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