473,320 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

create global variables?

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

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

Oct 30 '06 #1
5 1931
Alistair King wrote:
Hi,

is there a simple way of creating global variables within a function?
#module global:

def f(atom):
global a
a=1
globals()[atom+'var']=2

def f():
a=b=1
globals().update(locals())

_global=sys.modules[__name__]

def f(atom):
_global.a = 1
setattr(_global,atom+'var', 2)

# all/app global

import myglobals

def f(atom):
myglobals.a=1
setattr(myglobals....)
your example application seems to point towards 'odd' use of these techniques.
-robert
Oct 30 '06 #2
Alistair King wrote:
is there a simple way of creating global variables within a function?
def foo(name):
globals()[name] = "xxx"
globals()[name + 'aa'] = "yyy"
globals()[name + 'ab'] = "zzz"
Oct 30 '06 #3
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
Oct 30 '06 #4
Wojciech Muła wrote:
>is there a simple way of creating global variables within a function?

def foo(name):
globals()[name] = "xxx"
globals()[name + 'aa'] = "yyy"
globals()[name + 'ab'] = "zzz"
that kind of coding is punishable by law in some jurisdictions.

</F>

Oct 30 '06 #5
Wojciech Muła wrote:
Alistair King wrote:
>>is there a simple way of creating global variables within a function?


def foo(name):
globals()[name] = "xxx"
globals()[name + 'aa'] = "yyy"
globals()[name + 'ab'] = "zzz"
Please note that this is a terrible programming practice. Anyone who
really thinks they need to do this should be thinking hard about whether
they are using Python correctly (though tere are occasionaly
requirements where the feature can legitimately be used).

It would be *much* easier and *much* better programming practice to
modify your code so the function returns a two-element tuple, which you
then assign to two variables in an unpacking assignment.

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

Oct 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
22
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.