0

I have two statements: 1) "In the variable explorer, the namespace content (all global object references) of the current console can be seen. A function can access all variables in the namespace content, without explicitly passing them as function arguments."

2) "Usually, only the arguments passed to the function are in your environment. Variables not passed as arguments are not necessarily available."

Is one of them correct or can somebody explain, to which objects Python functions DO have access? Thanks!

Sylvi0202
  • 901
  • 2
  • 9
  • 13

2 Answers2

0

Functions have access to __builtins__, globals (which is actually scoped in the module), and locals which includes arguments.

See Short Description of the Scoping Rules?

Étienne Bersac
  • 580
  • 6
  • 11
0

In the variable explorer, the namespace content (all global object references) of the current console can be seen. A function can access all variables in the namespace content, without explicitly passing them as function arguments.

I'd say this is pretty much correct, although lacking.

It is correct when it says a function has accesses to all of the programs global names. If Python cannot find a name used in the current local scope, it assumes the name is global and will attempt to retrieve its value from there.

However, one point the above definition does not mention is builtin names. They are always accessible at any point in the program anywhere.

Usually, only the arguments passed to the function are in your environment. Variables not passed as arguments are not necessarily available.

I'd disagree with the claim made that only variables passed in as arguments are part of the functions local scope (a.k.a environment). If a variable is not declared global using the global or nonlocal statements, any variable created in a functions local scope belongs to that scope and can be accessed throughout that scope.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87