修改locals()有多安全? [英] How safe is modifying locals()?

查看:68
本文介绍了修改locals()有多安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种模仿by-reference参数传递给Python中的
immutables的方法。我需要这样做,因为我正在编写一个

自动VB到Python转换器。


这里是一个VB代码示例:


子变化(ByVal x,ByRef y)

x = x + 1

y = y + 1

结束子


x = 0:y = 0

更改x,y

''现在x应为0且y应为是1


建议的一种方法是使用locals()和间接

通过生成的字典引用不可变的内容,


def change(x,y,refs):

x = x + 1

refs [y] = refs [y] + 1


x = 0; y = 0;

更改(x,''y'',locals())

Python文档发出警告,修改<的内容br />
locals()可能不会影响本地值。尽管有警告,代码

似乎按照预期工作,至少在Python 2.2上。


有谁知道这种方法有多安全,特别是在

多线程环境?这是否适用于Jython?


有没有人有任何替代策略来尝试实现

目标? Pythonic是指Pythonic。方法如,


def change(x,y):

x = x + 1

y = y + 1

返回y


x = 0; y = 0

y = change(x,y)


在单线程环境中工作但是,因为''y''的值

在错误的时间发生变化,多线程有可能

两个'y'用户可能不同意其价值。


Paul

Paul Paterson

(pa**********@users.sourceforge.net)


vb2py :: Visual Basic到Python转换工具包
http://vb2py.sourceforge .net

I am trying to find a way to mimic by-reference argument passing for
immutables in Python. I need to do this because I am writing an
automated VB to Python converter.

Here''s an example of the VB code:

Sub Change(ByVal x, ByRef y)
x = x+1
y = y+1
End Sub

x = 0: y = 0
Change x, y
'' Now x should be 0 and y should be 1

One approach that has been suggested is to use locals() and indirect
references to the immutable via the resulting dictionary,

def change(x, y, refs):
x = x + 1
refs[y] = refs[y] + 1

x = 0; y = 0;
change(x, ''y'', locals())

The Python documentation gives a warning that modifying the contents of
locals() may not affect local values. Despite the warning, the code
seems to work as desired, at least on Python 2.2.

Does anyone know how safe this approach is, particularly in a
multithreaded environment? Will this work on Jython?

Does anyone have any alternative strategies for trying to achieve the
objective? A "Pythonic" approach such as,

def change(x, y):
x = x + 1
y = y + 1
return y

x = 0; y = 0
y = change(x, y)

Works in a single threaded environment but, because the value of ''y''
changes at the wrong time, with multiple threads there is a possibility
that two users of ''y'' could dissagree on its value.

Paul
Paul Paterson
(pa**********@users.sourceforge.net)

vb2py :: A Visual Basic to Python Conversion Toolkit
http://vb2py.sourceforge.net

推荐答案



" Paul Paterson" < PA ********** @ users.sourceforge.net>在消息中写道

新闻:BD ********************* @ twister.austin.rr.com ...

"Paul Paterson" <pa**********@users.sourceforge.net> wrote in message
news:BD*********************@twister.austin.rr.com ...
我试图找到一种方法来模仿Python中不可变的传递参考参数。我需要这样做,因为我正在编写一个自动VB到Python转换器。

这是VB代码的一个例子:

Sub Change( ByVal x,ByRef y)
x = x + 1
y = y + 1
End Sub

x = 0:y = 0
更改x,y
''现在x应为0,y应为1

建议的一种方法是使用locals()和间接
引用不可变的via由此产生的字典,

def change(x,y,refs):
x = x + 1
refs [y] = refs [y] + 1

x = 0; y = 0;
更改(x,''y'',locals())

Python文档提供警告,修改locals()的内容
可能不会影响当地价值观。尽管有警告,但代码似乎按照需要工作,至少在Python 2.2上。
I am trying to find a way to mimic by-reference argument passing for
immutables in Python. I need to do this because I am writing an
automated VB to Python converter.

Here''s an example of the VB code:

Sub Change(ByVal x, ByRef y)
x = x+1
y = y+1
End Sub

x = 0: y = 0
Change x, y
'' Now x should be 0 and y should be 1

One approach that has been suggested is to use locals() and indirect
references to the immutable via the resulting dictionary,

def change(x, y, refs):
x = x + 1
refs[y] = refs[y] + 1

x = 0; y = 0;
change(x, ''y'', locals())

The Python documentation gives a warning that modifying the contents of locals() may not affect local values. Despite the warning, the code
seems to work as desired, at least on Python 2.2.




在模块范围内,locals()== globals() 。在函数中,locals()

当前是函数的本地命名空间的*副本*。所以上面

只有在你从全局(模块)上下文调用

时才有效,如果你通过在函数内调用来测试它就不行。


Terry J. Reedy



At module scope, locals() == globals(). Within a function, locals()
currently is a *copy* of the function''s local namespace. So above
only works because you made the call from the global (module) context
and would not if you tested by making call from within a function.

Terry J. Reedy


Terry Reedy写道:
Terry Reedy wrote:
" Paul Paterson" < PA ********** @ users.sourceforge.net>在消息中写道
"Paul Paterson" <pa**********@users.sourceforge.net> wrote in message

Python文档发出警告,修改内容

The Python documentation gives a warning that modifying the contents





of

locals()可能不会影响本地值。尽管有警告,但代码似乎按照需要工作,至少在Python 2.2上。
locals() may not affect local values. Despite the warning, the code
seems to work as desired, at least on Python 2.2.



在模块范围内,locals()== globals()。在函数中,locals()
当前是函数的本地命名空间的*副本*。所以上面
只有在你从全局(模块)上下文中调用时才有效,如果你通过在函数内调用来测试它就不行。


At module scope, locals() == globals(). Within a function, locals()
currently is a *copy* of the function''s local namespace. So above
only works because you made the call from the global (module) context
and would not if you tested by making call from within a function.




谢谢特里!我调整了我的测试,现在看到了这个行为。

函数本身有效,但更改不会传播到

调用范围。所以这种使用locals()的方法似乎已经死了。


还有其他方法吗?


Paul



Thanks Terry! I adjusted my tests and now see this behaviour exactly.
The function itself works but the changes do not propogate out to the
calling scope. So this approach of using locals() appears to be dead.

Are there any other approaches?

Paul


Quoth Paul Paterson:

[...]
Quoth Paul Paterson:
[...]
有没有人有任何替代策略来试图实现
目标? Pythonic是指Pythonic。方法如,

def change(x,y):
x = x + 1
y = y + 1
返回y

x = 0; y = 0
y =更改(x,y)

在单线程环境中工作但是,因为''y''
的值在错误的时间发生变化,有多个线程的可能性有两个y的用户可能不同意它的价值。
Does anyone have any alternative strategies for trying to achieve the
objective? A "Pythonic" approach such as,

def change(x, y):
x = x + 1
y = y + 1
return y

x = 0; y = 0
y = change(x, y)

Works in a single threaded environment but, because the value of ''y''
changes at the wrong time, with multiple threads there is a possibility
that two users of ''y'' could dissagree on its value.




即使简单

y = y + 1

可能会改变来电者的约束力,会有竞争条件

- 无论如何你都需要锁定。 (如果这个

在VB中也不是这样的话我会有点惊讶。在VB中语句是否是原子的?)


-

Steven Taschuk w_w
st******@telusplanet.net , - = U

1 1



Even if the simple
y = y + 1
could change the caller''s binding, there would be a race condition
-- you''ll need a lock anyway. (I''d be a bit surprised if this
were not so in VB as well. Are statements atomic in VB?)

--
Steven Taschuk w_w
st******@telusplanet.net ,-= U
1 1


这篇关于修改locals()有多安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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