Python 传递参数 - 一个迷茫的初学者
Python passing arguments - a confused beginner
在更大的自学计划中遇到问题后,我在下面制作了一个快速脚本来测试一些行为。我正在使用 python 2.7.x.
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y
y = int(raw_input('Enter: '))
test(y)
print 'Outside the function', y
Enter: 6
Inside the function 36
Outside the function 6
但是,当代码如下:
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y
y = test(6)
print 'Outside the function', y
Inside the function 36
Outside the function 36
为什么第一个代码片段提供了 36, 6 而 而不是 36, 36,就像第二种情况一样?您可以为函数提出什么建议 return 更改后的值(在本例中为 36),以便可以将值传递给另一个函数。
对于上下文,我的目标是让用户输入一个值,将该值发送给一个函数。我希望该函数对该输入执行一些逻辑,例如测试以确保它满足特定条件:使用字符 [a-zA-z -],然后 return 该值,以便它可以传递到另一个功能。但是,我并不是在寻求支持
非常感谢您的宝贵时间,非常感谢您的帮助。
因为您通过设置 y = test(6) 更改了第二个示例中 y 的值。首先设置 y = test(y) ,你会得到相同的结果。
这两个片段之间的唯一区别是,在第二个片段中,您将 y
重新分配给调用函数的结果。这就是您需要做的。
在第一个代码中,您有一个名为 y
的全局变量,您将值 6 赋给它。进入该函数时,您创建了一个局部变量,也称为 y
,您将值 6*6=36 赋给该变量。当你离开函数时,你 return 这个值,但不使用它。然后,打印全局 y
。
在第二个代码中,您将 returned 值(本地 y
= 36)分配给您的全局值 y
,从而使两个打印值相同.
Why does the first code snippet provide 36, 6 and not 36, 36 as in the second case?
因为你定义了一个全新的y变量,其值为36,同时函数外的值为6。
在第二种情况下,你仍然有两个 y 变量,但是靠近底部的那个(几乎说第一个,因为它在技术上首先执行)是方法的 return 值
您没有在代码中将 test(y)
的结果分配给变量 y
y=int(raw_input('Enter: '))
# y= 6
# here you need to assign y= test(y)
test(y)
# test result is 36, but not saved to y
print 'Outside the function', y
# print 6
案例 #1 解释
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y; # returning 36
y=int(raw_input('Enter: ')) # here y is 6 as you read from user
test(y) # you are passing 6 and which computes y to be 36, though test is returning 36, but it is not stored in any variable (including y). (not re-assigned, so outside the definition/fucntion, y is still 6)
print 'Outside the function', y # so it is still 6
案例 #2 解释
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y; # returning 36
y=test(6) # here you received and stored it in y
print 'Outside the function', y # now y is 36
输出不一样的原因有两个:
- 范围不同,在您的第一个示例中,您在函数中分配的 y 与函数外部的 y not 相同,即使它们具有相同的名称。为了(过度)简化,您可以认为,当您开始一个新函数时,会创建一个全新的内存 space 并且在其中,没有任何东西已经存在(除了导入的模块名称,参数到函数,...),所以,即使你在函数内部对 y 进行赋值,它也是一个全新的 y,并且,当函数结束时,它的生命就停止了。
- 在第二个示例中,您获取函数的输出并将其放入 y 中,在第一个示例中,输出因未使用而丢失。
为了说明我的第一点:
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y;
test(3)
print y
这会引发 NameError,因为在函数之外,y 不存在。
我邀请您阅读并理解这个非常简单的解释Getting Started with Python - Variable scope
在更大的自学计划中遇到问题后,我在下面制作了一个快速脚本来测试一些行为。我正在使用 python 2.7.x.
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y
y = int(raw_input('Enter: '))
test(y)
print 'Outside the function', y
Enter: 6
Inside the function 36
Outside the function 6
但是,当代码如下:
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y
y = test(6)
print 'Outside the function', y
Inside the function 36
Outside the function 36
为什么第一个代码片段提供了 36, 6 而 而不是 36, 36,就像第二种情况一样?您可以为函数提出什么建议 return 更改后的值(在本例中为 36),以便可以将值传递给另一个函数。
对于上下文,我的目标是让用户输入一个值,将该值发送给一个函数。我希望该函数对该输入执行一些逻辑,例如测试以确保它满足特定条件:使用字符 [a-zA-z -],然后 return 该值,以便它可以传递到另一个功能。但是,我并不是在寻求支持
非常感谢您的宝贵时间,非常感谢您的帮助。
因为您通过设置 y = test(6) 更改了第二个示例中 y 的值。首先设置 y = test(y) ,你会得到相同的结果。
这两个片段之间的唯一区别是,在第二个片段中,您将 y
重新分配给调用函数的结果。这就是您需要做的。
在第一个代码中,您有一个名为 y
的全局变量,您将值 6 赋给它。进入该函数时,您创建了一个局部变量,也称为 y
,您将值 6*6=36 赋给该变量。当你离开函数时,你 return 这个值,但不使用它。然后,打印全局 y
。
在第二个代码中,您将 returned 值(本地 y
= 36)分配给您的全局值 y
,从而使两个打印值相同.
Why does the first code snippet provide 36, 6 and not 36, 36 as in the second case?
因为你定义了一个全新的y变量,其值为36,同时函数外的值为6。
在第二种情况下,你仍然有两个 y 变量,但是靠近底部的那个(几乎说第一个,因为它在技术上首先执行)是方法的 return 值
您没有在代码中将 test(y)
的结果分配给变量 y
y=int(raw_input('Enter: '))
# y= 6
# here you need to assign y= test(y)
test(y)
# test result is 36, but not saved to y
print 'Outside the function', y
# print 6
案例 #1 解释
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y; # returning 36
y=int(raw_input('Enter: ')) # here y is 6 as you read from user
test(y) # you are passing 6 and which computes y to be 36, though test is returning 36, but it is not stored in any variable (including y). (not re-assigned, so outside the definition/fucntion, y is still 6)
print 'Outside the function', y # so it is still 6
案例 #2 解释
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y; # returning 36
y=test(6) # here you received and stored it in y
print 'Outside the function', y # now y is 36
输出不一样的原因有两个: - 范围不同,在您的第一个示例中,您在函数中分配的 y 与函数外部的 y not 相同,即使它们具有相同的名称。为了(过度)简化,您可以认为,当您开始一个新函数时,会创建一个全新的内存 space 并且在其中,没有任何东西已经存在(除了导入的模块名称,参数到函数,...),所以,即使你在函数内部对 y 进行赋值,它也是一个全新的 y,并且,当函数结束时,它的生命就停止了。 - 在第二个示例中,您获取函数的输出并将其放入 y 中,在第一个示例中,输出因未使用而丢失。
为了说明我的第一点:
#!/usr/bin/python
def test(arg1):
y = arg1 * arg1
print 'Inside the function', y
return y;
test(3)
print y
这会引发 NameError,因为在函数之外,y 不存在。
我邀请您阅读并理解这个非常简单的解释Getting Started with Python - Variable scope