Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Nested Functions in Python: Scope and Accessibility | Understanding Variable Scope in Python
Intermediate Python

bookNested Functions in Python: Scope and Accessibility

This topic will not only help understand the nonlocal scope but also closures and decorators.

Functions are first-class citizens in Python. They can be:

  • Passed as arguments to functions;
  • Returned from functions;
  • Modified;
  • Assigned to variables.

Let's explore some examples:

def outer_function(...):
	...
    def inner_function(...):
		...
	return ...

In programming, a nested function is a function that is defined inside another function.

123456
def count_percent(num1, num2, num3): def inner(num): return num * 30 / 100 return (inner(num1), inner(num2), inner(num3)) print(count_percent(700, 300, 1000))
copy

Useful if you want to perform a complex task multiple times within another function without repeating code.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain what the output of the code will be?

How does the inner function work in this example?

Can you show another example using nested functions?

Awesome!

Completion rate improved to 3.7

bookNested Functions in Python: Scope and Accessibility

Deslize para mostrar o menu

This topic will not only help understand the nonlocal scope but also closures and decorators.

Functions are first-class citizens in Python. They can be:

  • Passed as arguments to functions;
  • Returned from functions;
  • Modified;
  • Assigned to variables.

Let's explore some examples:

def outer_function(...):
	...
    def inner_function(...):
		...
	return ...

In programming, a nested function is a function that is defined inside another function.

123456
def count_percent(num1, num2, num3): def inner(num): return num * 30 / 100 return (inner(num1), inner(num2), inner(num3)) print(count_percent(700, 300, 1000))
copy

Useful if you want to perform a complex task multiple times within another function without repeating code.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4
some-alt