Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Question: Evaluate an string variable's value to a variable

Reply
Thread Tools

Question: Evaluate an string variable's value to a variable

 
 
Mir Nazim
Guest
Posts: n/a
 
      12-21-2008
Just a quick question.

For example I have

>>> class X
>>> ....pass


Then I do
>>> x = X()
>>> x.name = 'Nazim


Now my question is whether something like below is possible and how
>>> y = 'name'
>>> print x.y # How can x.y can be evaluated to x.name



PS: In PHP this can be done by a $x->$y. I sure there is some way in
Python also


Thanks in advance.
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      12-21-2008
On Sat, Dec 20, 2008 at 10:49 PM, Mir Nazim <(E-Mail Removed)> wrote:
> Just a quick question.
>
> For example I have
>
>>>> class X
>>>> ....pass

>
> Then I do
>>>> x = X()
>>>> x.name = 'Nazim

>
> Now my question is whether something like below is possible and how
>>>> y = 'name'
>>>> print x.y # How can x.y can be evaluated to x.name


print getattr(x, y) #==> Nazim

There are also analogous setattr() and delattr() functions.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
 
 
 
Mir Nazim
Guest
Posts: n/a
 
      12-21-2008
On Dec 21, 11:56*am, "Chris Rebert" <(E-Mail Removed)> wrote:
> On Sat, Dec 20, 2008 at 10:49 PM, Mir Nazim <(E-Mail Removed)> wrote:
> > Just a quick question.

>
> > For example I have

>
> >>>> class X
> >>>> ....pass

>
> > Then I do
> >>>> x = X()
> >>>> x.name = 'Nazim

>
> > Now my question is whether something like below is possible and how
> >>>> y = 'name'
> >>>> print x.y * # *How can x.y can be evaluated to x.name

>
> print getattr(x, y) #==> Nazim
>
> There are also analogous setattr() and delattr() functions.
>


Damn Right.
These functions simply eluded my mind for well 15 minutes

Thanks a lot Chris.

> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I Locate a value in a text file and evaluate it and then writeout that line based on the value? kwalike57 Perl Misc 7 01-03-2008 06:20 PM
Cand XSLT evaluate XPath in String variable? Son KwonNam XML 4 01-05-2005 08:47 AM
How can I evaluate a XSLT param/variable during a Xalan C++ transformation ? philips XML 2 07-17-2004 08:28 PM
Evaluate as Variable, not String? Dave B. ASP General 1 06-14-2004 03:46 PM
evaluate a variable in JSTL EL Steven M. Lewis Java 0 05-08-2004 04:37 PM



Advertisments