Python:如何更改函数的输入参数的值? [英] python: how to change the value of function's input parameter?

查看:96
本文介绍了Python:如何更改函数的输入参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在函数内部修改字符串的值,如下所示:

I tried to modify the value of a string inside a function, like below:

>>> def appendFlag(target, value):
...     target += value
...     target += " "
...
>>> appendFlag
<function appendFlag at 0x102933398>
>>> appendFlag(m,"ok")
>>> m
''

好吧,似乎目标"仅在函数内部更改,但是如何使新值在函数外部可行?谢谢.

Well, seems the "target" is only changed within the function, but how to make the new value viable outside the function? Thanks.

推荐答案

这在python中通过返回进行处理.

This is handled in python by returning.

def appendFlag(target, value):
   target += value
   target += " "
   return target

您可以像这样使用它:

m = appendFlag(m,"ok")

您甚至可以返回几个这样的变量:

you can even return several variables like this:

def f(a,b):
   a += 1
   b += 1
   return a,b

并像这样使用它:

a,b = f(4,5)

这篇关于Python:如何更改函数的输入参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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