liveBook · Manning

Environment

As explained in Get Programming: Learn to code with Python

An environment is the external context, including its entities and conditions, in which a system, process, or agent exists or operates.

An environment is a separate working space for a program or task. A code module has its own environment: items created inside it stay there unless passed to another module through input or output. section 20.3

Each function call creates a new environment, also called a scope. Its parameters receive the call’s inputs, and variables created during the call belong to that environment. A return value carries a result back to the larger program. section 21.2 section 21.1

A function call’s working space

Calling word_length("Rob", "Banks") gives that call its own word1 and word2 parameters and its local word variable. The function returns the length; its local environment does not become part of the calling code. section 21.2 section 21.1

Figure 21.1. What happens when you make a function call in ? With and , the first parameter is mapped in the function’s scope. With and , the second parameter is mapped. is another variable created inside the function. is the return value. After the function returns, the function scope disappears along with all its variables.

Trace how a function call maps inputs to parameters, creates local variables, returns a value, and then ends its scope. section 21.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Traces function calls through parameter mapping, local variables, returned values, and the end of a scope.
Examples
Uses independent researchers in a group project and a word-length function call to illustrate separate working spaces.
What you can do after
Understand which variables belong to a function call and how inputs and return values cross its boundary.
How it connects
Connects environments with modules, functions, scope, parameters, and return values.

Also explained in

Compare how 8 books explain environment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.