通过引用错误自动调试复制? [英] Automatic debugging of copy by reference errors?

查看:45
本文介绍了通过引用错误自动调试复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个模块允许我通过引用找到由于复制而发生的错误

?我正在寻找以下内容:

Is there a module that allows me to find errors that occur due to copy
by reference? I am looking for something like the following:


>> import mydebug
mydebug.checkcopybyreference = True
a = 2
b = [a]
a = 4
>>import mydebug
mydebug.checkcopybyreference = True
a=2
b=[a]
a=4



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

CopyByReferenceError:变量b指变量a ,所以请不要

更改变量a。


这样的模块是否存在?

是否可以对此进行编码一个模块?

是否可以在

numpy中添加阵列复制功能?

用什么额外费用内存和CPU电源?


我意识到这个模块会禁用

语言的一些有用功能。另一方面,它可能对新的python用户有所帮助。


Niels

Traceback (most recent call last):
File "<stdin>", line 1, in ?
CopyByReferenceError: Variable b refers to variable a, so please do not
change variable a.

Does such a module exist?
Would it be possible to code such a module?
Would it be possible to add the functionality to array-copying in
numpy?
What would be the extra cost in terms of memory and CPU power?

I realize that this module would disable some useful features of the
language. On the other hand it could be helpful for new python users.

Niels

推荐答案

在9 dic,02:22,Niels L Ellegaard < niels.ellega ... @ gmail.comwrote:
On 9 dic, 02:22, "Niels L Ellegaard" <niels.ellega...@gmail.comwrote:

是否有一个模块可以让我找到因复制而发生的错误

参考?
Is there a module that allows me to find errors that occur due to copy
by reference?



你是什么意思按引用复制?

What do you mean by "copy by reference"?


我正在寻找像以下内容:
I am looking for something like the following:

> import mydebug
mydebug.checkcopybyreference = True
a = 2 b = [a]
a = 4
>import mydebug
mydebug.checkcopybyreference = True
a=2
b=[a]
a=4



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

CopyByReferenceError:变量b是指变量a,所以请不要更改变量a b
$ b。

Traceback (most recent call last):
File "<stdin>", line 1, in ?
CopyByReferenceError: Variable b refers to variable a, so please do not
change variable a.



(我不会因为Python没有变量而迂腐)。该代码有什么错误?b $ b错误?你是*不*改变变量a,你是

将整数4绑定到名称a。之前使用的名称是

绑定到另一个整数,2 - 它有什么问题?无论如何,它对名称b所引用的列表没有

效果,仍然保持b == [2]

你想要什么?禁止重复使用姓名?所以一旦你说a = 2,你就不能修改b $ b了吗?这可以做到,是的,一次写入多次读取

名称空间。但是我没有看到它的用处。

(I won''t be pedantic to say that Python has no "variables"). What''s
wrong with that code? You are *not* changing "variable a", you are
binding the integer 4 to the name "a". That name used previously to be
bound to another integer, 2 - what''s wrong with it? Anyway it has no
effect on the list referred by the name "b", still holds b==[2]
What do you want? Forbid the re-use of names? So once you say a=2, you
can''t modify it? That could be done, yes, a "write-once-read-many"
namespace. But I don''t see the usefullness.


这样的模块是否存在?
Does such a module exist?




No


是否可以对这样的模块进行编码?
Would it be possible to code such a module?



我不知道你想做什么,但我觉得这不是很有用的。

I don''t know what do you want to do exactly, but I feel it''s not
useful.


是否可以在

numpy中添加数组复制功能?
Would it be possible to add the functionality to array-copying in
numpy?



不知道。

No idea.


内存和CPU功率方面的额外成本是多少?
What would be the extra cost in terms of memory and CPU power?



还不知道。

No idea yet.


我意识到这个模块会禁用

语言的一些有用功能。
I realize that this module would disable some useful features of the
language.



不仅一些有用的功能,大多数程序甚至都不起作用!

Not only "some useful features", most programs would not even work!


另一方面,它可能对新的python用户有所帮助。
On the other hand it could be helpful for new python users.



我觉得你遇到麻烦了,你试图再次避免它......但也许这不是正确的办法。你能提供一些

的例子吗?


-

Gabriel Genellina

I think you got in trouble with something and you''re trying to avoid it
again - but perhaps this is not the right way. Could you provide some
example?

--
Gabriel Genellina


Gabriel Genellina写道:
Gabriel Genellina wrote:

我觉得你遇到麻烦了,你试图避免它

再次 - 但也许这不是正确的方法。你能提供一些

的例子吗?
I think you got in trouble with something and you''re trying to avoid it
again - but perhaps this is not the right way. Could you provide some
example?



我一直在使用scipy一段时间,但在开始时我通过引用复制了一些错误。下面的例子是

exagerated

为清晰起见,但原理是一样的:


import os

输出= []

firstlines = [0,0]

os.listdir中的文件名(''。''):

try:

firstlines [0] = open(filename," r")。readlines()[0]

firstlines [1] = open(filename, " r")。readlines()[1]

output.append((filename,firstlines))

除外:继续

打印输出


现在我的一些使用fortran的朋友想用python来分析他们的数据文件。我希望他们避免犯同样的错误,因为我这样做了,所以我认为如果他们能得到一些类似于保姆的警告那就好了:小心年轻人。如果这样做,那么
python将表现出与fortran和matlab不同的行为。这可能会让他们以正确的方式做事。


我不是这方面的专家,但我猜想有可能

制作一组约束,可以捕捉到如上所述的一些简单的

错误,但仍允许相当多的

编程。


Niels

I have been using scipy for some time now, but in the beginning I made
a few mistakes with copying by reference. The following example is
exagerated
for clarity, but the principle is the same:

import os
output=[]
firstlines =[0,0]
for filename in os.listdir(''.''):
try:
firstlines[0] = open(filename,"r").readlines()[0]
firstlines[1] = open(filename,"r").readlines()[1]
output.append((filename,firstlines))
except:continue
print output

Now some of my fortran-using friends would like to use python to
analyze their data files. I wanted them to avoid making the same
mistakes as I did so I thought it would be good if they could get some
nanny-like warnings saying: "Watch out young man. If do this, then
python will behave differently from fortran and matlab". That could
teach them to do things the right way.

I am not an expert on all this, but I guessed that it would be possible
to make a set of constraints that could catch a fair deal of simple
errors such as the one above, but still allow for quite a bit of
programming.

Niels


In< 11 *********** ***********@n67g2000cwd.googlegroups .com> ;, Niels L

Ellegaard写道:
In <11**********************@n67g2000cwd.googlegroups .com>, Niels L
Ellegaard wrote:

我有现在已经使用scipy了一段时间,但是在开始时我通过引用复制了几个错误。
I have been using scipy for some time now, but in the beginning I made
a few mistakes with copying by reference.



但是按引用复制是Python的工作方式。 Python永远不会复制

对象,除非你明确要求它。所以你想要的是一个警告

* *每个*任务。


Ciao,

Marc''BlackJack''Rintsch

But "copying by reference" is the way Python works. Python never copies
objects unless you explicitly ask for it. So what you want is a warning
for *every* assignment.

Ciao,
Marc ''BlackJack'' Rintsch


这篇关于通过引用错误自动调试复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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