Python - 传递值如何工作? [英] Python - how does passing values work?

查看:79
本文介绍了Python - 传递值如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Python中函数调用的问题。假设我想写一个名为superLongFunc(expr)的函数。该功能超长,真正难以调试。我想把这个函数分成更小的帮助函数以提高可读性,比如smallFunc1(expr),smallFunc2(expr)等等。



我的问题是,这是不是影响代码的性能呢?调用函数到底如何在Python中工作? Python是否通过引用将变量传递给函数?还是在将它提供给函数之前创建变量的副本?



我知道这是一个非常不好的问题,但它一直在困扰着我。提前致谢!

解决方案

Python使用有时称为调用 - 对象。将参数传递给函数时不会复制任何东西。函数参数的名称在函数体内局部绑定到函数调用中提供的相同对象。



这与大多数人认为的按值调用,因为它不会复制对象。但它与引用调用不同,因为引用是指向对象 ---绑定了新的名称,但是指向了同一个对象。这意味着你可以改变传入的对象,但重新命名函数内部的名字对函数没有影响。一个简单的例子:

 >>> def func(x):
... x [0] = 2#突变对象影响函数外部的对象
>>> myList = [1]
>>> func(myList)
>>> myList#myList已更改
[2]
>>> def func(x):
... x = 2#重新绑定名称在函数外部没有作用
>>> myList = [1]
>>> func(myList)
>>> myList#myList不受影响
[1]

我对此的简单思考就是赋值为一个裸名---也就是 name = value ---这样的语句与Python中的所有其他语句完全不同。 仅用于 name = value 的名称而不是值。 (这里有个挑剔的例外,比如像使用 globals()等等,但这些都是危险的领域。)特别是 name = value 不同于 obj.prop = value obj [0] = value obj + = value ,以及其他类似的东西,它们看起来像是赋值,但实际上是在对象上运行,而不是在名称上运行。 $ b

也就是说,Python中的函数调用本身具有一定的开销(用于设置执行框架等)。如果多次调用某个函数,则此开销可能会对性能造成显着影响。因此,将一个函数分解为多个函数仍然会对性能产生影响,因为每个附加函数调用都会增加一些开销。


I have a question about function calls in Python. Say I want to write a function called superLongFunc(expr). The function is super long and really hard to debug. I want to split the function into smaller helper functions for better readability, to something like smallFunc1(expr),smallFunc2(expr), etc.

My question is, does this affect the performance of the code at all? How exactly does calling functions work in Python? Does Python pass the variables to a function by reference? Or does it make a copy of the variable before feeding it to the function?

I know it's a pretty nooby question but it's been bugging me for a while. Thanks in advance!

解决方案

Python uses a system sometimes called call-by-object. Nothing is copied when you pass arguments to a function. The names of the function arguments are locally bound within the function body, to the same objects provided in the function call.

This is different from what most people think of as "call by value", because it doesn't copy the objects. But it's also different from "call by reference" because the reference is to the object --- a new name is bound, but to the same object. This means that you can mutate the passed-in object, but rebinding the name inside the function has no effect outside the function. A simple example of the difference:

>>> def func(x):
...     x[0] = 2 # Mutating the object affects the object outside the function
>>> myList = [1]
>>> func(myList)
>>> myList # myList has changed
[2]
>>> def func(x):
...     x = 2 # rebinding name has no effect outside the function
>>> myList = [1]
>>> func(myList)
>>> myList # myList is unaffected
[1]

My simple way of thinking about this is that assignment to a bare name --- that is, statements of the form name = value --- is completely different from everything else in Python. The only way to operate on names and not on values is to do name = value. (There are nitpicky exceptions to this, like mucking around with globals() and so on, but these are dangerous territory anyway.) In particular name = value is different from obj.prop = value, obj[0] = value, obj += value, and other similar things that look like assignment but actually operate on objects and not on names.

That said, function calls in Python have a certain amount of overhead just in themselves (for setting up the execution frame, etc.). If a function is called many times, this overhead can cause a noticeable performance impact. So splitting one function into many could still have a performance impact, since each additional function call will add some overhead.

这篇关于Python - 传递值如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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