简体   繁体   English

在python中将相同的参数从外部函数传递给内部函数

[英]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.

相关问题 将外部 function 的参数作为内部 function 的参数传递给 python? - passing argument of outer function as argument for inner function in python? 当外部函数具有同名的关键字参数时,将关键字参数传递给内部函数 - passing keyword argument to inner function when outer function has keyword argument with the same name 在Python中将内部函数的返回值传递给外部函数? - Passing the returned value of the inner function to the outer function in Python? 如何从外部函数获取参数? Python - how to get an argument from an outer function? python 从python中的内部函数对象获取外部函数的名称 - Get name of outer function from inner function object in python 在Python中,是否可以使用内部函数体内的外部函数变量? - Is there a way to use variable from outer function in the inner function body, in Python? Maya Python:从内部函数访问外部函数变量 - Maya Python: Accessing outer function variable from inner function 带有参数的python内部函数 - python inner function with argument 为 Python 中的 function 传递相同的参数名称 - Passing same argument names for a function in Python 在 Python 中将多个关键字参数从外部函数传递到内部函数? - relay multiple keyword arguments from outer function to inner functions in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM