[英]passing same argument from outer function to inner function in python
I have a inner function function我有一个内部函数函数
def inner_function(scenario = 'name',myopic_variable = True):
if scenario == 'name':
print('Name')
if myopic_variable == True:
print('Myopic is True')
and the main function like this.和这样的主要功能。
def main_function(scenario = 'name',myopic_variable = True):
inner_function()
Now when I am calling main function like this it works fine现在,当我像这样调用 main 函数时,它工作正常
main_function(scenario = 'name',myopic_variable = True)
but when i change myopic_variable = False it still gives me the same output why?但是当我更改 myopic_variable = False 时,它仍然给我相同的输出,为什么?
def inner_function(scenario = 'name',myopic_variable = True):
You are passing a default value of true here. def inner_function(scenario = 'name',myopic_variable = True):
您在此处传递默认值 true。
def main_function(scenario = 'name',myopic_variable = True):
inner_function()
Here, no parameters are passed to inner_functions()
, it prints True
, the default value.在这里,没有参数传递给
inner_functions()
,它打印True
,默认值。 To make it correct, call inner_function(myopic_variable = myopic_variable)
要使其正确,请调用
inner_function(myopic_variable = myopic_variable)
Well that's because you are using something called default arguments in python.那是因为你在 python 中使用了一种叫做默认参数的东西。 To understand what you are facing, let's take an example:
为了了解您所面临的问题,让我们举个例子:
def inner_fun(a=True):
print(a)
inner_fun()
will print True because we are not passing any argument while calling this function.将打印 True 因为我们在调用此函数时没有传递任何参数。 It will thus takr the default argument which is True here.
因此,它将 takr 此处为 True 的默认参数。
If you run inner_fun(False)
, it'll print False because this time you are passing an argument in the function which has higher precedence than the default value.如果您运行
inner_fun(False)
,它将打印 False 因为这次您在函数中传递了一个优先级高于默认值的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.