简体   繁体   English

Python函数,覆盖原始变量

[英]Python function, overwrite original variable

I have this function: 我有这个功能:

def icaocode(code):
    c.execute("SELECT ICAO, LAT, LON FROM airports WHERE ICAO = ?", (code,))
    result = c.fetchone()
    if result is None:
        print("No airport found with ICAO code", code)
        sys.exit()
    else:
        print("Found", code)
        [...]

Lets say I call this function with 可以说我用

icaocode(x)

How do I get the function to overwrite x with the results? 如何获得将结果覆盖x的函数?

In function def: 在函数def中:

def icaocode(code):
    ...
    return code  # new value

When calling: 致电时:

x = icaocode(x)

Btw if the argument is mutable (like a list), you can overwrite it without returning the new value. 顺便说一句,如果参数是可变的 (如列表),则可以覆盖它而无需返回新值。 If it's immutable (like a string, integer), you can't. 如果它是不可变的(例如字符串,整数),则不能。

Eg 例如

def f(some_list):
    some_list.append("something")

In this case 在这种情况下

my_list = []
f(my_list)

my_list will be ["something"] my_list将是["something"]

You can't overwrite the value of the parameter. 您不能覆盖参数的值。 That is, you can't change it to refer to another object. 也就是说,您不能将其更改为引用另一个对象。 You can, however, change the object. 但是,您可以更改对象。 There is an old thread on pass-by-value and pass-by-reference semantics in Python that you may find illuminating: https://stackoverflow.com/a/986145/399047 Python中有一个关于传值和传引用语义的旧线程,您可能会发现有启发性: https : //stackoverflow.com/a/986145/399047

For example, you can append elements to a list that is passed in as a parameter. 例如,您可以将元素追加到作为参数传递的列表中。 The following code: 如下代码:

def func(a_list):
    a_list.append("some value")

l = [1,2,3]
print l
func(l)
print l

would give: 会给:

[1,2,3]
[1,2,3,"some value"]

In contrast, a string, cannot be modified. 相反,不能修改字符串。 The following code: 如下代码:

def func2(a_str):
    a_str += "suffix"

s = "test"
print s
func2(s)
print s

would give: 会给:

"test"
"test"

My recommendation, unless you have a good reason, is to avoid mutating your input parameters, and return the modified object instead. 除非您有充分的理由,否则我的建议是避免更改您的输入参数,而是返回修改后的对象。 Side-effects can make for messy code. 副作用可能会使代码混乱。

If, at the end of all this you really want to modify your input parameter, one technique would be to wrap the code parameter inside another object. 如果在所有这些操作的最后,您确实想修改输入参数,则一种方法是将代码参数包装在另一个对象中。 eg 例如

def icaocode(code_list):
    input_code = code_list[0]
    [...]
    # do something to input_code, assign result to
    # output_code
    [...]
    code_list[0] = output_code

Then you would call with: 然后,您将致电:

code_list = [code]
icaocode(code_list)

That said, this code is ugly to me, smells something awful, and I don't recommend it. 就是说,这段代码对我来说很丑陋,闻起来很糟,我不建议这样做。

You can, but it is a horrible way to conduct business. 您可以,但这是开展业务的可怕方式。 Return the value instead, remember that you can return more than one value if you like. 而是返回该值,请记住,如果您愿意,可以返回多个值。 Here is however one way to return a value through a parameter. 但是,这是通过参数返回值的一种方法。 But don't use it. 但是不要使用它。

>>> def a(b):
...     b[0] = 'hi'
....
>>> c = ['nono']
>>> a(c)
>>> print(c)
['hi']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM