Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Local Variables in Python: Understanding Function-Level Scope | Understanding Variable Scope in Python
Intermediate Python Techniques
course content

Kursusindhold

Intermediate Python Techniques

Intermediate Python Techniques

1. Mastering Packing and Unpacking in Python
2. Mastering Function Arguments in Python
3. Function as an Argument in Python
4. Understanding Variable Scope in Python
5. Mastering Python Decorators

book
Local Variables in Python: Understanding Function-Level Scope

A local variable is defined inside a function. You cannot access this variable outside the function's definition.

123456
def greet(): message = "Aloha" print(message, "# local message") greet() print(message) # This line will result in an error
copy

The output is:

python

The message variable is not accessible in the global scope; it exists only inside the function. When the function exits, this variable ceases to exist.

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2
Vi beklager, at noget gik galt. Hvad skete der?
some-alt