[Tutor] inquire

Blake Winton bwinton@tor.dhs.org
Thu, 11 Jan 2001 21:05:56 -0500


Doug wrote:
> XIA Xiao-Qin wrote:
> > Does python offer the function like the "&" in FoxPro? 
> > i.e. in foxpro, some code like following:
> >  
> >             a="I am a"
> >             b="a"
> >             print &b
> >  
> > may print the content of variable a.
> > Can one do like this in python?
> I don't think Python would do it in exactly that way.  I'm sure there is a
> "Pythonic" way to do what you want.  Try to describe the problem you're
> trying to solve and we'll probably be able to help.

I very much agree with Doug, in that it's not really a Pythonic
way of doing things, but you could use something like :

Python 1.5.2 (#1, Sep 17 1999, 20:15:36)
[GCC egcs-2.91.66 19990314/Linux (egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> a = "I am a"
>>> b = "a"

#Now you can use either:

>>> l = locals()
>>> l.get(b)
'I am a'

#Or, alternately: 

>>> eval(b)
'I am a'


But, I plead with you, as someone who may, someday in the future, read
your code, or code from someone who might do something like that,
don't use that construct.  It makes your code _very_ hard to read, and
there's usually a better way to do what you want.

Good luck,
Blake.