create global variables?

Steve Holden steve at holdenweb.com
Tue Oct 31 05:01:28 EST 2006


Alistair King wrote:
> Steve Holden wrote:
> 
>>aking at mappi.helsinki.fi wrote:
>>  
>>
>>>J. Clifford Dyer wrote:
>>>
>>>    
>>>
>>>>Alistair King wrote:
>>>>      
>>
>>[... advice and help ...]
>>
>>  
>>
>>>this worked a treat:
>>>
>>>def monoVarcalc(atom):
>>>
>>>   a = atom + 'aa'
>>>   Xaa = a.strip('\'')
>>>   m = atom + 'ma'
>>>   Xma = m.strip('\'')
>>>   Xaa = DS1v.get(atom)
>>>   Xma = pt.get(atom)
>>>   return Xaa, Xma
>>>
>>>
>>>Caa, Cma = monoVarcalc('C')
>>>
>>>    
>>
>>In which case I suspect you will find that this works just as well:
>>
>>def monoVarcalc(atom):
>>
>>    Xaa = DS1v.get(atom)
>>    Xma = pt.get(atom)
>>    return Xaa, Xma
>>
>>
>>Unless there is something decidedly odd about the side-effects of the 
>>statements I've removed, since you never appear to use the values of a, 
>>m, Xaa and Xma there seems little point in calculation them.
>>
>>regards
>>  Steve
>>  
> 
> Yup...it works..but now i have to create a dictionary of 'a' and 'm',
> ie... "Xaa" and "Xma" string, key:value pairs so i can use other
> functions on the Xaa, Xma variables by iterating over them and
> retrieving the values from the variables. I think if i just input Xaa
> and Xma, only the values associated with those variables will go into
> the dictionary and ill just be iterating over nonsence.....
> 
> atomsmasses = {}
> 
> def monoVarcalc(atom):
> a = atom + 'aa'
> m = atom + 'ma'
> atomsmasses[a]=m
> Xaa = a.strip('\'')
> Xma = m.strip('\'')
> Xma = pt.get(atom)
> if DS1v.get(atom) != None:
> Xaa = DS1v.get(atom)
> else:
> Xaa = 0
> return Xaa, Xma
> 
> Caa, Cma = monoVarcalc('C')
> Oaa, Oma = monoVarcalc('O')
> Haa, Hma = monoVarcalc('H')
> Naa, Nma = monoVarcalc('N')
> Saa, Sma = monoVarcalc('S')
> Claa, Clma = monoVarcalc('Cl')
> Braa, Brma = monoVarcalc('Br')
> Znaa, Znma = monoVarcalc('Zn')
> 
> 
> 
> i think? :)
> thanks
> 
> a
> 
No fair: you only just added atomsmasses! ;-)

However, it seems to me that your atomsmasses dictionary is going to be 
entirely predictable, and you are still focusing on storing the *names* 
of things rather than building up a usable data structure. Indeed I 
suspect that your problem can be solved using only the names of the 
elements, and not the names of the variables that hold various 
attributes of the elements.

Perhaps if you explain in plain English what you *really* want to do we 
can help you find a more Pythonic solution. It'll probably end up 
something like this:

mass = {}
for element in ['C', 'O', ..., 'Zn']
     mass[element] = monoVarcalc(element)

But I could, of course, be completely wrong ... it wouldn't be the first 
time. Do you understand what I'm saying?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list