Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52900 > unrolled thread

Re: Python variable as a string

Started byVlastimil Brom <vlastimil.brom@gmail.com>
First post2013-08-23 18:13 +0200
Last post2013-08-23 18:13 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python variable as a string Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-08-23 18:13 +0200

#52900 — Re: Python variable as a string

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2013-08-23 18:13 +0200
SubjectRe: Python variable as a string
Message-ID<mailman.170.1377274427.19984.python-list@python.org>
2013/8/23 Jake Angulo <jake.angulo@gmail.com>:
> Sorry this is a very basic question.
>
> I have a list var which after some evaluation I need to refer to var as a
> string.
>
> Pseudocode:
>
> var = ['a', 'b' , 'c' , 'd']
> adict = dict(var='string', anothervar='anotherstring')
> anotherdict = dict()
> if <condition>:
>     anotherdict[akey] = adict['var']
>
>
> Basically im evaluating the list var, and if true, i want to use var as a
> string so that i can refer to a key-value pair in adict (whose key name is
> also var for convenience).
>
> Or maybe i should do things differently?
>
> Any help and code will be appreciated!
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Hi,
if I understand correctly, you would like to achieve something like
the following [fictional shell session]:

>>> var = "a string variable named 'var'"
>>> some_magic_function(var)
'var'
>>>

is it the case?
I believe, it is not (easily and reliably) possible, and especially it
doesn't seem to be of any real use, I can think of.
cf. e.g. the discussion
http://stackoverflow.com/questions/2553354/how-to-get-a-variable-name-as-a-string-in-python

What is your use case, where you can't use the actual object
reference, but only the string? What name string should be retrieved
in case of multiple names referencing the same object?


hth,
   vbr

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web