某个对象的引用或指针? [英] reference or pointer to some object?

查看:84
本文介绍了某个对象的引用或指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想将一个参考或指针传递给一个对象

到一个函数。该函数应该更改

对象,并且更改应该在调用

函数中可见。


在Perl中这将是类似于:


sub func {

$ ref = shift;


$$ ref + = 123 ; #change

}


$ a = 1;

func(\ $ a);


在python中是这样的吗?


关键字global不符合这个目的

我的理解,因为它只会使得UPPERMOST级别的变量可见,而不是一次性的

调用以上级别。


这是否可以用weakref实现?


我不想将参数传递给函数和

然后返回一个更改的值。


python中是否还有其他一些机制可用于实现这样的行为?

感谢任何提示,

Torsten。

Hi,

i''d like to pass a reference or a pointer to an object
to a function. The function should then change the
object and the changes should be visible in the calling
function.

In perl this would be something like:

sub func {
$ref = shift;

$$ref += 123; # change
}

$a = 1;
func(\$a);

is something like this possible in python?

The keyword "global" does NOT fit this purpose to
my understanding as it only makes the variables of
the UPPERMOST level visible, not the ones of ONE
calling level above.

Is this somehow possible with weakref?

I don''t want to pass the parameter to a function and
then return a changed value.

Is there some other mechanism in python available to
achieve a behaviour like this?
Thanks for any hints,
Torsten.

推荐答案

ref = shift;

ref = shift;


ref + = 123; #change

}

ref += 123; # change
}


a = 1;

func(\
a = 1;
func(\


这篇关于某个对象的引用或指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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