On Thu, Dec 8, 2011 at 11:03 AM, Steven D'Aprano
<[email protected]> wrote:
>> It is really important that the scope of a,b,c is limited to the Sum
>> function, they must not exisit outside it or inside any other nested
>> functions.
>
> The second part is impossible, because that is not how Python works.
> Nested functions in Python can always see variables in their enclosing
> scope. If you don't want that behaviour, use another language, or don't
> use nested functions.
To the OP: By "nested functions", did you mean actual nested functions
(those defined inside this function), or simply functions called from
this one?
This is a nested function, as the term is usually taken to mean:
def outer_function():
a = 1
def inner_function():
b = 2
return a+b
print(inner_function()) # Prints 3
The inner function has access to all of the outer function's
namespace. But if you meant this:
def function_1():
b = 2
return a+b
def function_2():
a = 1
print(function_1())
then it's quite the other way around - Python never shares variables
in this way (this would have function_1 assume that 'a' is a global
name, so if it's not, you'll get a run-time NameError).
As Steven says, there's no way in Python to hide variables from an
actual nested function. But if you just mean a function called from
this one, then what you want is the normal behaviour (and if you
actually want to share variables, you pass them as arguments).
ChrisA
--
http://mail.python.org/mailman/listinfo/python-list