如何更改函数中变量的范围?Python [英] How to change the scope of a variable in a function? Python

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

问题描述

这似乎是一个非常愚蠢的问题,但是我对Python的作用域规则感到困惑.在下面的示例中,我将带有值的两个变量(x,y)发送到应该更改其值的函数.当我打印结果时,变量没有改变.

This may seem like a really stupid question but I am confused regarding the scope rules in Python. In the following example I send two variables (x,y) with values to a function which is supposed to change their values. When I print the results the variables had not changed.

def func1(x,y):
    x=200
    y=300

x=2
y=3

func1(x,y)

print x,y #prints 2,3

现在,如果这是C ++,我将通过引用(&)将其发送给该函数,从而可以更改其值.那么,Python中的等价之处是什么?更重要的是,将对象发送给函数时实际发生了什么?Python是否对这些对象进行了新引用?

Now if this were C++ I would send them by reference (&) to that function and therefore be able to change their values. So what's the equivilant in Python? and more important, what actually happens when you send objects to function? does Python make new references to these objects?

推荐答案

将它们视为函数的一部分.该函数结束时,其所有变量也将消失.

Think of them as being part of the function. When the function ends, all its variables die too.

x=2
y=3

def func(x,y):
    x=200
    y=300

func(x,y) #inside this function, x=200 and y=300
#but by this line the function is over and those new values are discarded
print(x,y) #so this is looking at the outer scope again

如果您希望函数完全按照编写值的方式修改值,则可以使用 global ,但这是非常糟糕的做法.

If you want a function to modify a value in exactly the way you have written it, you could use a global but this is VERY bad practice.

def func(x,y):
    global x #these tell the function to look at the outer scope 
    global y #and use those references to x and y, not the inner scope
    x=200
    y=300

func(x,y)
print(x,y) #prints 200 300

问题是,在最佳情况下它会使调试成为一场噩梦,而在最坏情况下则完全不可能进行调试.诸如此类的事情在函数中通常被称为副作用"-设置不需要设置的值并且在没有显式返回的情况下这样做是一件坏事.通常,您应该编写的用于就地修改项目的唯一函数是对象方法(像 [].append()这样的东西修改列表,因为返回一个新列表很愚蠢!)

The problem with this is that it makes debugging a nightmare in the best case, and utterly incomprehensibly impossible in the worst case. Things like these are commonly known as "side effects" in functions -- setting a value you don't need set and doing so without explicitly returning it is kind of a Bad Thing. Generally the only functions you should write that modify items in-place are object methods (things like [].append() modify the list because it's silly to return a new list instead!)

执行类似操作的正确方法是使用返回值.尝试类似

The RIGHT way to do something like this would be to use a return value. Try something like

def func(x,y):
    x = x+200 #this can be written x += 200
    y = y+300 #as above: y += 300
    return (x,y) #returns a tuple (x,y)

x = 2
y = 3
func(x,y) # returns (202, 303)
print(x,y) #prints 2 3

为什么没有用?好吧,因为您从未告诉程序使用该元组(202,303)做任何事情,只是为了计算它.让我们现在分配它

Why didn't that work? Well because you never told the program to DO anything with that tuple (202, 303), just to calculate it. Let's assign it now

#func as defined above

x=2 ; y=3
x,y = func(x,y) #this unpacks the tuple (202,303) into two values and x and y
print(x,y) #prints 202 303

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

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