Sam <[email protected]> writes:
> I need to pass a global variable into a python function.
Python does not really have the concept "variable".
What appears to be a variable is in fact only the binding of an
object to a name. If you assign something to a variable,
all you do is binding a different object to the name.
Thus, if you have:
i = 1
def f(x): x = 5
f(i)
Then "i" will remain "1" and not become "5".
The effect of "x = 5" is that the name "x" gets bound to "5"
(where is formerly was bound to "1").
>However, the global variable does not seem to be assigned after the function
>ends. Is it because parameters are not passed by reference?
Python lacks the notion of "variable". Thus, it does not
pass variables into functions but objects.
The objects, however, get passed by reference.
>How can I get function parameters to be passed by reference in Python?
You can implement your own concept of variable, e.g. like this:
class Variable(object):
def __init__(self, value): self.value = value
The little program above then becomes:
i = Variable(1)
def f(x): x.value = 5
f(i)
i.value # this is now 5
--
https://mail.python.org/mailman/listinfo/python-list