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


Groups > comp.lang.python > #52900

Re: Python variable as a string

References <CAH7RoS773x000QWuJ3+UZ--CQLkHzD6Sa_dTA7Mx3Fg=A1CJfw@mail.gmail.com>
Date 2013-08-23 18:13 +0200
Subject Re: Python variable as a string
From Vlastimil Brom <vlastimil.brom@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.170.1377274427.19984.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web