K
K
Kozhanov Maxim2020-05-30 17:35:24
Python
Kozhanov Maxim, 2020-05-30 17:35:24

How to call a function within a function?

The code:

>>> def a():
  a = 5
  def b():
      nonlocal a 
      print(a)

Can you please tell me how to call the b() function?
Of course, I understand that you can add a call to this function b () at the end.
Like this:
def a():
  a = 5
  def b():
      nonlocal a 
      print(a)
  b()

but can it be done without this command at the end?
Since for my program I need to call the b () function a little later.
Tell me please)))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
galaxy, 2020-05-30
@MaxPython375

def a():
  a = 5
  def b():
      nonlocal a 
      print(a)
  return b

fb = a()
fb()

K
Kristal1, 2020-05-30
@Kristal1

through the display statement

M
Maxim, 2020-05-30
@XTerris

Just put b() where you need to call this function.
Do you need to call this function in the body of function a or somewhere else?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question