更新时间:2023-02-19 12:48:44
greenflame写道:
我想创建一个执行以下操作的函数。我将其称为
thefunc。
> s =" Char"
thefunc(s)
s
''|| Char>>''
我尝试了以下
def thefunc(s):
s =" ||" + s +">>"
问题是,如果我在将函数
应用到它之后查看字符串,那么没有修改。我意识到我在变量的
范围内遇到了问题。函数s中的字符串是
函数的本地字符串,因此我不会更改输入的字符串,而是更改
副本。我似乎无法弄清楚如何得到我想要的东西。感谢
你的时间。
你不能直接做你想做的事。字符串是
不可变对象。一旦创建了一个字符串,该字符串就不能被修改为
。当您对字符串进行操作时,会产生不同的
字符串。对字符串进行操作的函数应返回其值:
>> def thefunc( s):
.... return''||''+ s +''>>''
....
>> s =' 'Char''
s = thefunc(s)
s
''|| Char>> ;''
有/几个hacks会做你想要的。但是,如果你确实需要它,那么你可能需要重新考虑你的程序设计。
记住,你不能改变字符串,因为字符串是不可变的!你
可以改变一个变量来绑定另一个字符串。在下面的
示例中,s会反弹到新字符串,而t保留原始的
字符串值:
>> def changeString(varName):
.... globalDict = globals()
.... globalDict [varName] =''||''+ globalDict [varName] +''>>''
....返回
....
>> s =''Char''
t = s
changeString('s'')
s
''|| Char>>''
>> t
''Char''
另请注意,这仅影响全局范围内的变量。我希望这会有所帮助!
- 贾森
>
greenflame写道:
我想创建一个执行以下操作的函数。我将其称为
thefunc。
> s =" Char"
thefunc(s)
s
''|| Char>>''
我尝试了以下
def thefunc(s):
s =" ||" + s +">>"
问题是,如果我在将函数
应用到它之后查看字符串,那么没有修改。我意识到我在变量的
范围内遇到了问题。函数s中的字符串是
函数的本地字符串,因此我不会更改输入的字符串,而是更改
副本。我似乎无法弄清楚如何得到我想要的东西。感谢
你的时间。
快速入侵
def thefunc(s):
return s =" | |" + s +">>"
>> s =" ;你好
s = thefunc(s)
print s
|| hello>>
- 欢呼
Jason写道:
>
你无法直接做你想做的事。字符串是
不可变对象。一旦创建了一个字符串,该字符串就不能被修改为
。当您对字符串进行操作时,会产生不同的
字符串。对字符串进行操作的函数应返回其值:
> def thefunc(s):
...返回''||''+ s +''>>''
...
> s =''Char''
s = thefunc(s)
s
''|| Char>>''
有/有几个hacks会做你想要的。但是,如果你确实需要它,那么你可能需要重新考虑你的程序设计。
记住,你不能改变字符串,因为字符串是不可变的!你
可以改变一个变量来绑定另一个字符串。在下面的
示例中,s会反弹到新字符串,而t保留原始的
字符串值:
> def changeString(varName):
... globalDict = globals()
... globalDict [varName] =''||''+ globalDict [varName] +''>>''
...返回
...
> s =''Char''
t = s
changeString ('s'')s
''|| Char>>''
> t
''Char''
另请注意,这仅影响全局范围内的变量。我希望这会有所帮助!
--Jason
好的让我看看是否我明白。 globalDict只是一个字典
包含全局变量的名称作为键,它们的
值作为字典的值?因此,输入变量是否像对待全局变量一样被视为
?
I want to make a function that does the following. I will call it
thefunc for short.
>>s = "Char"
thefunc(s)
s
''||Char>>''
I tried the following
def thefunc(s):
s = "||" + s + ">>"
The problem is that if I look at the string after I apply the function
to it, it is not modified. I realized that I am having issues with the
scope of the variables. The string in the function, s, is local to the
function and thus I am not changing the string that was inputed, but a
copy. I cannot seem to figure out how to get what I want done. Thank
you for your time.
greenflame wrote:I want to make a function that does the following. I will call it
thefunc for short.>s = "Char"
thefunc(s)
s
''||Char>>''
I tried the following
def thefunc(s):
s = "||" + s + ">>"
The problem is that if I look at the string after I apply the function
to it, it is not modified. I realized that I am having issues with the
scope of the variables. The string in the function, s, is local to the
function and thus I am not changing the string that was inputed, but a
copy. I cannot seem to figure out how to get what I want done. Thank
you for your time.You cannot do what you are trying to do directly. Strings are
immutable objects. Once a string is created, that string cannot be
modified. When you operate on a string, you produce a different
string. Functions which operate on a string should return their value:>>def thefunc(s):
.... return ''||'' + s + ''>>''
....>>s = ''Char''
s = thefunc(s)
s
''||Char>>''
There /are/ a few hacks which will do what you want. However, if you
really need it, then you probably need to rethink your program design.
Remember, you can''t change a string since a string is immutable! You
can change a variable to bind to another string. In the following
example, s gets rebound to the new string while t keeps the original
string value:>>def changeString(varName):
.... globalDict = globals()
.... globalDict[varName] = ''||'' + globalDict[varName] + ''>>''
.... return
....>>s = ''Char''
t = s
changeString(''s'')
s
''||Char>>''
>>t
''Char''
Further note that this only affects variables in the global scope. I
hope this helps!
--Jason
greenflame wrote:I want to make a function that does the following. I will call it
thefunc for short.>s = "Char"
thefunc(s)
s
''||Char>>''
I tried the following
def thefunc(s):
s = "||" + s + ">>"
The problem is that if I look at the string after I apply the function
to it, it is not modified. I realized that I am having issues with the
scope of the variables. The string in the function, s, is local to the
function and thus I am not changing the string that was inputed, but a
copy. I cannot seem to figure out how to get what I want done. Thank
you for your time.quick hack
def thefunc(s):
return s = "||" + s + ">>">>s = "hello"
s = thefunc(s)
print s
||hello>>
--Cheers
Jason wrote:>
You cannot do what you are trying to do directly. Strings are
immutable objects. Once a string is created, that string cannot be
modified. When you operate on a string, you produce a different
string. Functions which operate on a string should return their value:>def thefunc(s):
... return ''||'' + s + ''>>''
...>s = ''Char''
s = thefunc(s)
s
''||Char>>''
There /are/ a few hacks which will do what you want. However, if you
really need it, then you probably need to rethink your program design.
Remember, you can''t change a string since a string is immutable! You
can change a variable to bind to another string. In the following
example, s gets rebound to the new string while t keeps the original
string value:>def changeString(varName):
... globalDict = globals()
... globalDict[varName] = ''||'' + globalDict[varName] + ''>>''
... return
...>s = ''Char''
t = s
changeString(''s'')
s
''||Char>>''
>t
''Char''
Further note that this only affects variables in the global scope. I
hope this helps!
--JasonOk so let me see if I understand. The globalDict is just a dictionary
containing the name of the global variables as the keys and their
values as the values of the dictionary? Thus the inputed variable is
treated like a global variable?