I know that in Python you can access the nested function by setting the value in a variable that contains the value of the parent function, like in a function. Is it possible to similarly access other functions contained in the parent?

def func1(x): def func2(y): return x, y def func3(z): return x, z return func2 return func3 new = func1(x) #new(y) == funct2(y) - тут показано как доступ можно получить к первой вложенной функции извне # ?? == func3(z) - вопрос в том как получить доступ ко второй вложеной функции извне (возможно ли это в Python?) 
  • 2
    func1 returns func2, which will be stored in the variable new. return func3 will never be executed. - Sergey Gornostaev
  • 2
    This can be done, but not always. What problem are you trying to solve? What is the context? See the XY task - jfs
  • Related question: Python double return - jfs
  • one
    Comrades ruling that returning outside a function is not correct, they need to be tightened up. But the fact that there are 2 of them is so conceived by the author of the question, he asks about it, no need to try to “fix” this. - insolor

3 answers 3

As far as I know, no, you can call a nested function only from the function of the parent, child, or from the function nested in the parent.

Is that so

 def fparent(x): def func(x): def fchild(x): return x + 1 return fchild return func new_func = fparent(0) new_fchild = new_func(0) >>> new_fchild(0) 1 

here new_func is a link to the func function inside fparent, and new_fchild is a link to fchild inside new_func.

    It is not completely clear what you want.

    You have func2 and func3 are absolutely identical. That is, one and the other get the variable x from the parent function and some other variable as its own argument, and then returns a pair from x and its argument.

    The fact that in one case you have an argument called y, and in the other case, z, does not change anything, because it is a purely internal function name. Externally, func2 and func3 will work ABSOLUTELY the same.

    Well, and you have a confusion with the arguments. When you declare a function, you can actually write any valid name with the argument, and not worry about where the argument comes from.

    But when you call the function func1 (x), you must first ensure that you already have the variable x.

    It seems that you do not really understand what functions are and how they work - I advise you to look into this topic.

    Now about your question.

    If I did understand your original intention correctly, and you need to have a container with two functions, then this can be done through classes.

     class Func1(object): def func2(self, param): print('Сработала функция func2 c параметром ' + param) def func3(self, param): print('Сработала функция func3 с параметром ' + param) y = func2 z = func3 new = Func1() new.y('3') # напечатает: Сработала функция func2 c параметром 3 new.z('3') # напечатает: Сработала функция func3 c параметром 3 
       def func1(x): def func2(y): return x, y def func3(z): return x, z yield func2, func3 # fn links def func4(a): return a, func2(x) yield func4(x) # fn return f = func1(1) func2, func3 = next(f) print(func2(2), func3(3)) f4 = next(f) print(f4)