Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > create global variables?

Reply
Thread Tools

create global variables?

 
 
aking@mappi.helsinki.fi
Guest
Posts: n/a
 
      10-31-2006


J. Clifford Dyer wrote:
> Alistair King wrote:
>
>> Hi,
>>
>> is there a simple way of creating global variables within a function?
>>
>> 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
>>
>>

>
> I suspect you are misusing the concept of a function. In most basic
> cases, and I suspect your case applies just as well as most, a function
> should take arguments and return results, with no other communication
> between the calling code and the function itself. When you are inside
> your function don't worry about the names of the variables outside. I'm
> not sure exactly where your floats are coming from, but try something
> like this:
>
> >>> def monoVarCalc(relevant_data):

> ... float1 = relevant_data * 42.0
> ... float2 = relevant_data / 23.0
> ... return float1, float2
>
> >>> C = 2001
> >>> Caa, Cab = monoVarCalc(C)
> >>> Caa

> 84042.0
> >>> Cab

> 87.0
>
> Notice that you don't need to use the variable C (or much less the
> string "C", inside monoVarCalc at all. It gets bound to the name
> relevant_data instead.
>
> Also, if you are going to have a lot of these little results lying
> around, (Cab, Cac ... Czy, Czz), you might consider making them a list
> or a dictionary instead. I won't tell you how to do that, though. The
> online tutorial has plenty of information on that.
>
> http://docs.python.org/tut/tut.html
>
>
> Cheers,
> Cliff
>

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')


thanks

Ali

--
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Could not load type 'Global.yaddayadda'. - Global.asax.cs Daniel Bass ASP .Net 2 12-31-2003 03:57 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Global Error handling in Applicatio_Error() of Global.asax VSK ASP .Net 1 07-29-2003 03:12 AM



Advertisments