Gary Herron wrote: > Alistair King wrote: > >> Hi, >> >> is there a simple way of creating global variables within a function? >> >> >> > Use the "global" statement within a function to bind a variable to a > global. > > See http://docs.python.org/ref/global.html for details. > > > >>>> def x(): >>>> > ... global g > ... g = 123 > ... > >>>> x() >>>> g >>>> > 123 > > > Gary Herron > > > > > >> ive tried simply adding the variables in: >> >> def function(atom, Xaa, Xab): >> Xaa = onefunction(atom) >> Xab = anotherfunction(atom) >> >> if i can give something like: >> >> function('C') #where atom = 'C' but not necessarly include Xaa or Xab >> >> i would like to recieve: >> >> Caa = a float >> Cab = another float >> >> ive tried predefining Xaa and Xab before the function but they are >> global values and wont change within my function. Is there a simple way >> round this, even if i call the function with the variables ('C', Caa, Cab)? >> .............................................................................................................................. >> >> some actual code: >> >> # sample dictionaries >> DS1v = {'C': 6} >> pt = {'C': 12.0107} >> >> def monoVarcalc(atom): >> a = atom + 'aa' >> Xaa = a.strip('\'') >> m = atom + 'ma' >> Xma = m.strip('\'') >> Xaa = DS1v.get(atom) >> Xma = pt.get(atom) >> print Xma >> print Xaa >> >> monoVarcalc('C') >> >> print Caa >> print Cma >> .............................................................................................................................. >> it seems to work but again i can only print the values of Xma and Xaa >> >> ? >> >> Alistair >> >> >> > > have tried:
def monoVarcalc(atom): a = atom + 'aa' Xaa = a.strip('\'') m = atom + 'ma' Xma = m.strip('\'') global Xma global Xaa Xaa = DS1v.get(atom) Xma = pt.get(atom) print Xma print Xaa monoVarcalc('C') print Caa print Cma ....................................................................................................... where global Xma & Xaa are before and after any further functions i get still get the error Traceback (most recent call last): File "DS1excessH2O.py", line 54, in ? print Caa NameError: name 'Caa' is not defined -- Dr. Alistair King Research Chemist, Laboratory of Organic Chemistry, Department of Chemistry, Faculty of Science P.O. Box 55 (A.I. Virtasen aukio 1) FIN-00014 University of Helsinki Tel. +358 9 191 50392, Mobile +358 (0)50 5279446 Fax +358 9 191 50366 -- http://mail.python.org/mailman/listinfo/python-list