How to do this? - newbie

Ronald L. Dilsavor dilsavor at erinet.com
Thu Feb 10 22:52:19 EST 2000


> From: Steve Holden <sholden at bellatlantic.net>
> Organization: Holden Web: Intranet Technology Specialists
> Reply-To: sholden at BellAtlantic.net
> Newsgroups: comp.lang.python
> Date: Thu, 10 Feb 2000 16:59:10 GMT
> Subject: Re: How to do this? - newbie
> 
> "Ronald L. Dilsavor" wrote:
>> 
>> Hi,
>> I am new to python and am quite excited about it. I spent much of last night
>> writing my fist code snippet and I came across 1 thing which I could not
>> figure out how to do.
>> 
>> Suppose
>> 
>>>>> a=('x',1)
>> then how can I create an x of numerical type such that
>>>>> print x
>> 1
>> 
>> - just using manipulations of a = ('x',1)
>> 
>> Also let me know if this type of thing would not be considered best practice
>> in python. I thought I would need to make use of eval() but could not find
>> an incantation that would do it.
>> 
>> Thanks,
>> Ron
> I am sure there are people reading this group who can tell you
> how to dynamically construct something which will create a
> variable called a and assign it the value 1.
> 
> But it seems you really want to be able to retrieve the numeric
> value using the symbol 'a' as a key, and that's what dictionaries
> are for.
> 
>>>> mydict={}  # empty dictionary
>>>> mydict['a']=1 # enter value 1 against key 'a'
>>>> print mydict['a'] # retrieve the value
> 1
>>>> 
> 
> Is *that* what you want?
> 
> regards
> Steve
> 

In fact that is exactly what I ended up doing (using a dictionary) and I may
just keep it that way. I was just wondering how to do it the way I asked.
Ron




More information about the Python-list mailing list