osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can global variable be passed into Python function?


On Thu, 20 Feb 2014 22:37:59 -0800, Sam wrote:

> I need to pass a global variable into a python function. However, the
> global variable does not seem to be assigned after the function ends. Is
> it because parameters are not passed by reference? How can I get
> function parameters to be passed by reference in Python?

You cannot. Python does not support either pass-by-reference or pass-by-
value calling conventions. Instead, it supports the same calling 
convention as Java, Ruby and other modern languages (which confusingly 
those two languages both call something different), "call by object 
sharing".

To understand more about Python's calling convention, and why neither 
call-by-reference nor call-by-value are appropriate, please read these 
articles:

https://mail.python.org/pipermail/tutor/2010-December/080505.html
http://effbot.org/zone/call-by-object.htm

and also see the Wikipedia article:

http://en.wikipedia.org/wiki/Evaluation_strategy


If you show us a sample of your code, together of a sample of how you 
expect it to work, we can suggest an alternate way to solve that problem.



-- 
Steven