[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

to create variable from dict

hiral

3/12/2010 1:59:00 PM

Hi,

Is there any way to create variables which name matches with dict key?

For example:
dict1 = {"abc":'1", "def":"2"}

Now I am looking to have variable name abc and it's value be '1' etc.

Pl. suggest.

Thank you.
5 Answers

Tim Chase

3/12/2010 2:41:00 PM

0

hiral wrote:
> Is there any way to create variables which name matches with dict key?
>
> For example:
> dict1 = {"abc":'1", "def":"2"}
>
> Now I am looking to have variable name abc and it's value be '1' etc.

1) you can't because "def" is a reserved word in Python.

2) why do you want to? This seems to come up about every week or
so and people STILL want to do it. Search the archives...you
won't have to go back too far.

3) once you have it, how do you plan to use the variables? If
you don't know what they'll be named ahead of time, how can you
use them in your code

The usual answer is "don't do that -- just use them as a dict",
or if you're trying to set up some constants, you can use

abc, def_, ghi = 1, 2, 3

-tkc







Luis M. González

3/12/2010 2:50:00 PM

0

On Mar 12, 10:59 am, hiral <hiralsmaill...@gmail.com> wrote:
> Hi,
>
> Is there any way to create variables which name matches with dict key?
>
> For example:
> dict1 = {"abc":'1", "def":"2"}
>
> Now I am looking to have variable name abc and it's value be '1' etc.
>
> Pl. suggest.
>
> Thank you.

Check out this thread (very recent):
http://groups.google.com/group/comp.lang.python/browse_thread/thread/bb1797ffb6fc3bd7/25fe94103c7a231f?lnk=gst&q=luis+variables#25fe94...

Short answer: you can update globals() with a dictionary, as follows:

globals().update( dict1 )

Then you'll have each key-value pair as variables in the global
namespace.
The question is: should you do it?

Luis

Jean-Michel Pichavant

3/12/2010 3:02:00 PM

0

Luis M. González wrote:
> On Mar 12, 10:59 am, hiral <hiralsmaill...@gmail.com> wrote:
>
>> Hi,
>>
>> Is there any way to create variables which name matches with dict key?
>>
>> For example:
>> dict1 = {"abc":'1", "def":"2"}
>>
>> Now I am looking to have variable name abc and it's value be '1' etc.
>>
>> Pl. suggest.
>>
>> Thank you.
>>
>
> Check out this thread (very recent):
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/bb1797ffb6fc3bd7/25fe94103c7a231f?lnk=gst&q=luis+variables#25fe94...
>
> Short answer: you can update globals() with a dictionary, as follows:
>
> globals().update( dict1 )
>
> Then you'll have each key-value pair as variables in the global
> namespace.
> The question is: should you do it?
>
> Luis
>
The answer is known: no, he should not do it :o)

JM

Andreas Waldenburger

3/13/2010 11:42:00 AM

0

On Fri, 12 Mar 2010 06:50:18 -0800 (PST) Luis M. González
<luismgz@gmail.com> wrote:

> The question is: should you do it?

And the answer is: No.

And the usual disclaimer is: (Unless you *know* it's the best possible
solution to your problem.)

/W

--
INVALID? DE!

hiral

3/15/2010 6:52:00 AM

0

On Mar 12, 8:02 pm, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:
> Luis M. González wrote:
> > On Mar 12, 10:59 am,hiral<hiralsmaill...@gmail.com> wrote:
>
> >> Hi,
>
> >> Is there any way to create variables which name matches with dict key?
>
> >> For example:
> >> dict1 = {"abc":'1", "def":"2"}
>
> >> Now I am looking to have variable name abc and it's value be '1' etc.
>
> >> Pl. suggest.
>
> >> Thank you.
>
> > Check out this thread (very recent):
> >http://groups.google.com/group/comp.lang.python/browse_thre......
>
> > Short answer: you can update globals() with a dictionary, as follows:
>
> > globals().update( dict1 )
>
> > Then you'll have each key-value pair as variables in the global
> > namespace.
> > The question is: should you do it?
>
> > Luis
>
> The answer is known: no, he should not do it :o)
>
> JM- Hide quoted text -
>
> - Show quoted text -

Thanks for your explanations.