scope of function parameters (take two)

Henry Olders henry.olders at mcgill.ca
Tue May 31 02:57:24 EDT 2011


On 2011-05-30, at 20:52 , Benjamin Kaplan wrote:

> On Mon, May 30, 2011 at 5:28 PM, Henry Olders <henry.olders at mcgill.ca> wrote:
>> 
>> On 2011-05-29, at 4:30 , Henry Olders wrote:
>> 
> 
> Python doesn't have true globals. When we say "global" what we mean is
> "module or built-in". Also, consider this code
> 
> from math import sin
> def redundant_sin(x) :
>    return sin(x)
> 
> In Python, everything is an object. That includes functions.  By your
> definition, that function would either have to be written as
> def redundant_sin(sin, x) :
> and you would have to pass the function in every time you wanted to
> call it or have a "global sin" declaration in your function. And you
> would need to do that for every single function that you call in your
> function body.
> 
I don't believe so. Within redundant_sin, x is local, so if I change x, it will not change any objects named x outside of the function. As far as sin is concerned, if it were passed to redundant_sin via the parameter list, then it would be local, but otherwise sin would be looked for in the function definition; if not found there, it would be looked for in the module, where it would be found. I am not suggesting any changes to how names are looked up or scoped.

Henry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110531/3bb17e7f/attachment-0001.html>


More information about the Python-list mailing list