January 31st, 2002, 04:23 AM
Defining a variable within a function as global
At the moment I have a function which gives a result, I need this result to use it in a different function. However, because the variable is defined within the function, it's scope is only local, therefore I cannot use it outside of the function. I cannot just define it outside of the function because all of the function creates this variable.
I have tried usine the following type of syntax:
struc_smi = f.read
however this still doesn't seem to work as I get errors saying that struc_smi is not defined.
February 1st, 2002, 03:18 AM
First of all: global is considered evil. Don't use it. It can lead to some very irritating debugging. You could just do it this way:
struc_smi = function()
foo = bar(struc_smi)
But, to fix the error, you should first define the variable outside of the function before using global inside a function. global doesn't define a variable, it makes it usable within the function.
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. - Jamie Zawinski, in comp.lang.emacs
October 30th, 2004, 04:31 AM
October 30th, 2004, 11:12 AM
I don’t see the problem, not that I really use globals so much; as Taradino said: evil . Anyway I've done you a little example:
The first example is obviously not use globals, but it does show the preferred way of doing this kind of thing [editing a the value of a variable]. One advantage of this, besides making code much more readable is that the function can also be used with other variables .
>>> def foo(argument):
return argument + 1
>>> def bar(argument):
return argument + 5
>>> globalVariable = foo(6)
>>> globalVariable = bar(globalVariable)
>>> def foo():
globalVariable += 1
>>> def bar():
globalVariable += 5
>>> globalVariable = 6
But its the second example you want to be looking at. As you can see, foo() and bar() can be defined before our globalVariable just as long as it is defined when the functions are called!
Hope you find this useful, enjoy!
October 30th, 2004, 03:44 PM
Thanks man. Much appreciated.
October 31st, 2004, 04:21 AM
Listen to Taradino, you don't want to use global variable unless you consider yourself a VB hating, god fearing, object oriented purist. I guess it might be ok if the application you are creating is very small and good programming practice is not necessary, but I highly doubt it.
Originally Posted by gaston9x19
I thought there would be a big OOP revelations were even schoolgirls know about good programming design. But i guess it was not to be, I guess that the same with career in programming but I am ganna just give up and let VB and Java programmers create bad code while , I am working as a crummy refusge collector , you have another thing coming.
October 31st, 2004, 04:28 AM
That isn't a problem because functions can return values, as Netytan demonstrates, but if you mean the type of the variable is decided by the function (integer, string, etc) that also isn't a problem - you can use type(x) to find out what type the returned variable is.
Although in that case, it could likely be re-written to avoid that sort of checking.
Well, apart from me not quite getting what you mean here, you might be in a situation where a custom class could help.
Objects are much nicer than global variables, really.
The value defined in __init__ (.speed) is available through the whole class (as a global would be). The functions in the class (speedUp, slowDown) can change and use it.
def __init__(self, mph):
self.speed = mph
self.speed += change
self.speed -= change
carSpeed = SpeedStore(mph=55)
bikeSpeed = SpeedStore(mph=10)
Outside the class, you can also get to any specific instance of it, using the carSpeed.speed form (unlike a global).
It's a bit like using a global, but without many of the problems -
e.g. you can have more than one instance of the class using it without conflicting names. You have to say "bikeSpeed.speed" instead of just "speed", so you're never confused about where the variable is coming from, or where it is defined. You can trace where it is being used, and how, much more easily than global variables. You don't need to define "global x" at the top of every function that uses it.
If you want to post some more sample code, it might be clearer what would help you...
October 31st, 2004, 08:52 AM
Personally I prefer to use isinstance() over type() since the type() function only works with the built-in types! Other types of variable will be labelled 'instance'. Observe .
Besides looking cleaner, this can be very useful .
>>> class Test1:
self.something = None
>>> class Test2:
self.something = None
>>> test0 = 'Im a string'
>>> test1 = Test1()
>>> test2 = Test2()
>>> type(test1) == type(Test2())
>>> isinstance(test1, Test1)
>>> isinstance(test2, Test2)
>>> isinstance(test1, Test2)