如何更改可选功能参数的默认值 [英] How to change default value of optional function parameter

查看:74
本文介绍了如何更改可选功能参数的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 b.py 更改 a.py 处的全局变量 S ,但是它在 a.py 处的函数.

I need to change the global variable S at a.py from b.py, but it is used as a default value in a function at a.py.

a.py

S = "string"


def f(s=S):
    print(s)
    print(S)

b.py

import a


def main():
    a.S = "another string"
    a.f()


if __name__ == "__main__":
    main()

python b.py 输出

string
another string

而不是预期的

another string
another string

如果我这样在 b.py 中调用 a.f

a.f(a.S)

这可以按预期工作,但是有什么方法可以更改默认变量值?

this works as expected, but is there any way to change default variable value?

推荐答案

简短的答案是:不能.

这样做的原因是,函数默认参数是在函数定义时创建的,并不意味着必须重新定义默认值.变量名称一次绑定到一个值,仅此而已,您不能将该名称重新绑定到另一个值.首先,让我们看一下全局范围内的变量:

The reason for this is that the function default arguments are created at function definition time, and the defaults are not meant to be re-defined. The variable name is bound once to a value and that is all, you can't re-bind that name to another value. First, let's look at variables in global scope:

# create a string in global scope
a = "string"

# b is "string"
b = a

a += " new" # b is still "string", a is a new object since strings are immutable

您现在已经将新名称绑定到字符串",而字符串新"是绑定到a的全新值,它不会更改b,因为 str + = str 返回a new str ,使 a b 引用不同的对象.

You've now just bound a new name to "string", and "string new" is a completely new value bound to a, it does not change b because str += str returns a new str, making a and b refer to different objects.

函数也是如此:

x = "123"

# this expression is compiled here at definition time
def a(f=x):
    print(f)

x = "222"
a()
# 123

变量 f 在定义时定义为默认值"123" .这是无法更改的.即使使用可变的默认值,例如问题:

The variable f was defined with the default of "123" at definition time. This can't be changed. Even with mutable defaults such as in this question:

x = []

def a(f=x):
    print(x)

a()
[]

# mutate the reference to the default defined in the function
x.append(1)

a()
[1]

x
[1]

已经定义了默认参数,并且名称 f 已绑定到值 [] ,该值不能更改.您可以更改与 f 关联的值,但是默认情况下您不能将 f 绑定到新值.为了进一步说明:

The default argument was already defined, and the name f was bound to the value [], that cannot be changed. You can mutate the value associated with f, but you cannot bind f to a new value as a default. To further illustrate:

x = []

def a(f=x):
    f.append(1)
    print(f)

a()
x
[1]

# re-defining x simply binds a new value to the name x
x = [1,2,3]

# the default is still the same value that it was when you defined the
# function, albeit, a mutable one
a()
[1, 1]

最好A)将全局变量作为函数的参数传递给B,或者B)将全局变量用作 global .如果要更改要使用的全局变量,请不要将其设置为默认参数,而是选择更合适的默认值:

It might be better to either A) pass the global variable in as an argument to the function or B) use the global variable as global. If you are going to change the global variable you wish to use, don't set it as a default parameter and choose a more suitable default:

# some global value
x = "some default"

# I'm choosing a default of None here
# so I can either explicitly pass something or
# check against the None singleton
def a(f=None):
    f = f if f is not None else x
    print(f)

a()
some default

x = "other default"
a()
other default

a('non default')
non default

这篇关于如何更改可选功能参数的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆