Generally in Python functions defined in functions are not directly callable, but what if you want to use them?
General:
def a():# First layer function def b():# Layer 2 functions print('Open file B') b()#Functions in the second level are called directly
The results show:
Traceback (most recent call last):
File "C:/Users/rog/Desktop/", line 4, in <module>
b()
NameError: name 'b' is not defined
But then you have to use it, what should you do? This time you can use the b function by simply returning the function name of the b function in function a.
def a():# First layer function def b():# Layer 2 functions print('Open file B') return(b) s=a()# First you have to call the a-function once and give the return value of the a-function to s, in this case also the b-function s()
Results:
Open file B
>>>
If multiple functions within the same function need to be called:
Here a global variable Position_number is set first, then this global variable is stated in a(), and then different functions in a() are called by the change in the global variable.
Position_number=0 def a():# First layer function global Position_number def b():# Layer 2 functions print('Open file B') def c(): print('Open file C') def d(): print('Open file D') if(Position_number==0): return(b) if(Position_number==1): return(c) if(Position_number==2): return(d) s=a() # First you have to call the a-function once and give the return value of the a-function to s, in this case also the b-function s() # Run the b function Position_number=1 # Change Position_number so that the return value of a() changes to the c function s=a() # Assign the c function to s s() # Run the c function Position_number=2 # Change Position_number so that the return value of a() changes to the c function s=a() # Assign the d function to s s() #(of a computer) rundfunction (math.)
Results:
Open file B
Open file C
Open file D
>>>
Additional knowledge:python learning: solving how to manipulate data within a function without affecting the original list
About a how to modify a third-order matrix within a function.
In python, if you want to define a function that passes in a list as a formal parameter in C++, it's obviously not possible. In python, there are only pass by reference and pass by value. When you pass in a value, you pass by value, and when you pass in a list or a dictionary, you pass by reference.
If a reference is passed to operate inside a function, the list or dictionary outside the function is changed as well. It seems that python has its share of inconveniences! What if we want to work with a matrix or a list?
After many trials, I finally found a way to do it. In python, the value of a dictionary type is immutable, while a list is changeable. And the conversion between them is list() and tuple(). So all we need is to be a dictionary type outside the function, pass it in and then give this dictionary type variable to a new dictionary type variable and convert it to a list type at the same time, then we can modify the list arbitrarily without affecting the original dictionary type data!
Here is a program on how to modify a third-order matrix within a function:
juzhen=((1,2,3),(4,5,6),(7,8,9)) def delju(juzhen): print(juzhen) a=list((juzhen)) for i in range(len(a)): a[i]=list(a[i]) print(a) for i in range(len(a)): del a[i][0] print(a) delju(juzhen) print(juzhen)
Results:
((1, 2, 3), (4, 5, 6), (7, 8, 9)) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[2, 3], [5, 6], [8, 9]] ((1, 2, 3), (4, 5, 6), (7, 8, 9)) >>>
It's only from the results above that we can see that when we pass it in, it's a two-dimensional dictionary, and then internally it changes to a two-dimensional list, and then operates on the list, and in the end it won't have any effect on the variables of the dictionary type!
So is it possible to have a list of every value in a dictionary type and then pass it to a function to modify the values within the list? The answer is no. Because the values within the dictionary are still in the form of lists, modifying them will still affect the lists within the dictionary type outside the function.
The following program:
juzhen=([1,2,3],[4,5,6],[7,8,9]) def delju(juzhen): print(juzhen) a=list((juzhen)) print(a) for i in range(len(a)): del a[i][0] print(a) delju(juzhen) print(juzhen)
Results:
([1, 2, 3], [4, 5, 6], [7, 8, 9]) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[2, 3], [5, 6], [8, 9]] ([2, 3], [5, 6], [8, 9]) >>>
From the results, we see that after the modification inside the function, the list inside the dictionary outside the function is also modified at the same time.
This illustrates how dictionaries can be used to solve the problem of handling lists within functions.
Method II:
Of course, there is a simpler way, we can directly introduce numpy first: import numpy as np, and then use the functions within numpy to directly deal with the matrix. It is also a recommended operation.
Above this Python how to call a function within a function way in main is all that I have shared with you, I hope to give you a reference, and I hope you support me more.