初学者问题 - 如何有效地传递大型列表 [英] Beginner question - How to effectively pass a large list

查看:44
本文介绍了初学者问题 - 如何有效地传递大型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,


python只能支持函数调用中的传递值(对吧?),我是

想知道如何有效传递一个大的参数,比如一个大的列表或

字典?


它可以通过C ++中的指针实现,在Python中是否有这样的方式?


Thansk提前。

JR

Hi folks,

The python can only support passing value in function call (right?), I''m
wondering how to effectively pass a large parameter, such as a large list or
dictionary?

It could achieved by pointer in C++, is there such way in Python?

Thansk in advance.
J.R.

推荐答案

JR写道:
嗨伙计们,

python只能支持在函数调用中传递值(对吧?),
Hi folks,

The python can only support passing value in function call (right?),




让我们看看:



let''s see :

l = range(5)
def modif(alist):alist [ 0] =''allo''
.... l
[0,1,2,3,4] modif(l)
l
[''allo' ',1,2,3,4]


呃...好吧,让我们试试别的:


class Toto :
....传递

.... t = Toto()
t.name =" Toto"
def rename(t):t .name =" Titi"
.... t
< __ main __。托托实例位于0x402dccec> t.name
''Toto''重命名(t)
t
< __ main __。托托实例位于0x402dccec> t.name
l = range(5)
def modif(alist): alist[0] = ''allo'' .... l [0, 1, 2, 3, 4] modif(l)
l [''allo'', 1, 2, 3, 4]

Er... Ok, let''s try something else :

class Toto: .... pass
.... t = Toto()
t.name = "Toto"
def rename(t): t.name = "Titi" .... t <__main__.Toto instance at 0x402dccec> t.name ''Toto'' rename(t)
t <__main__.Toto instance at 0x402dccec> t.name



''Titi''

嗯......你可能想了解更多关于绑定的信息(不是''assignements'')

Python。


(提示:它已经*确实像C [++]中的指针一样工作 - 至少对于

可变对象)。


布鲁诺


''Titi''
Well... You may want to read more about bindings (not ''assignements'') in
Python.

(hint : it already *does* work like pointers in C[++] - at least for
mutable objects).

Bruno


2003年12月15日星期一15:14:48 +0800,JR写道:
On Mon, 15 Dec 2003 15:14:48 +0800, J.R. wrote:
python只能支持函数调用中的传递值(对吗?)
The python can only support passing value in function call (right?)




错误。 Python中的函数参数总是通过引用传递,

不是按值传递的。 local函数参数绑定到传递的相同

对象,因此

内存中没有对象的两个副本。


当然,由于Python处理赋值的方式,你可以

然后更改函数中的本地参数,它将重新绑定

到新的价值,而没有改变原来的价值。


-

\我去了一家名为''DéjàVu'的高档法国餐馆。头|

` \服务员说,''我不认识你?'' - Steven Wright |
_o__)|

Ben Finney< http://bignose.squidly.org/>



Wrong. Function parameters in Python are always passed by reference,
not by value. The local function parameter gets a binding to the same
object that was passed, so there are not two copies of the object in
memory.

Of course, because of the way that Python treats assignment, you can
then change that local parameter within the function and it will re-bind
to the new value, without altering the original value passed.

--
\ "I went to a fancy French restaurant called ''Déjà Vu''. The head |
`\ waiter said, ''Don''t I know you?''" -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>


在文章< 3f *********************** @ news.free.fr> ;,

Bruno Desthuilliers< bd *********** @ removeme.free.fr>写道:

....
In article <3f***********************@news.free.fr>,
Bruno Desthuilliers <bd***********@removeme.free.fr> wrote:
....
(提示:它已经*确实像C [++]中的指针一样工作 - 至少对于
可变对象而言) 。
(hint : it already *does* work like pointers in C[++] - at least for
mutable objects).




对于任何和所有对象,不论其是否可变。


Donn Cave,做** @ u.washington.edu


这篇关于初学者问题 - 如何有效地传递大型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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