奇怪的方法keyword-arg bug。 [英] Bizarre method keyword-arg bug.

查看:115
本文介绍了奇怪的方法keyword-arg bug。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难过。我正在调用一个有关键字args的方法,但是没有

设置它们,然而其中一个从数据开始?!


班级定义从这样开始:


类BattleIntentionAction(BattleAction):

def __init __(self,factionName,location,tactic =''hold'',

targetFacName ='''',terrainArgs = [],garrisonIds = []):

self.terrainArgs = terrainArgs

print terrainArgs

构造函数在其他地方调用,如下所示:

act = BattleIntentionAction(facName,self.location)

在此对象期间''构造,terrainArgs设置为一个列表,其中

值对应于之前创建的BattleIntentionAction!

更奇怪的是,terrainArgs参数是一种测试形式,并且

实际上并没有在我的代码中的任何地方使用 - 相应的

属性总是在创建对象后被修改。此外,这个

与其他关键字args没有发生...


显然,我在这里掩盖了大量的代码,但是我有一个很难解决这个问题的时间,因为它似乎非常依赖于导致它的
事件。感觉就像那种内存踩踏

我记得在我攻击C ++的时候看到的错误。 :-(

我坦率地不明白terrainArgs如果可以有一个值如果

在调用调用时没有传递任何值,缺少一些

模糊编译器错误(这是Python 2.4.3)。我是天真的吗?

有什么方法我可以把这个带给自己吗?


我可以通过让调用者明确地设置

terrainArgs来轻松解决这个奇怪的问题,但是我不能动摇这个感觉这个

"修复只是掩盖我代码中的一些更深层次的缺陷。

Arg!

-Jasper

解决方案

< blockquote> 2008/8/18 Jasper< ja **** @ peak.org>:


我很难过。我正在调用一种方法有关键字args,但没有

设置它们,然而其中一个从数据开始?!



< http:/ /www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects>


-

干杯,

Simon B.
si *** @ brunningonline.net
http://www.brunningonline.net/simon/blog/

GTalk:simon.brunning | MSN:small_values |雅虎:小值| Twitter:brunns


Jasper schrieb:


我很难过。我正在调用一个有关键字args的方法,但是没有

设置它们,然而其中一个从数据开始?!


班级定义从这样开始:


类BattleIntentionAction(BattleAction):

def __init __(self,factionName,location,tactic =''hold'',

targetFacName ='''',terrainArgs = [],garrisonIds = []):

self.terrainArgs = terrainArgs

print terrainArgs

构造函数在其他地方被调用,如下所示:

act = BattleIntentionAction(facName,self.location)


期间这个对象的构造,terrainArgs设置为一个列表,其中

值对应于之前创建的BattleIntentionAction!

更奇怪的是,terrainArgs参数是一种测试形式,并且

实际上并没有在我的代码中的任何地方使用 - 相应的

属性总是在obje之后被修改ct创作。此外,这个

与其他关键字args没有发生...


显然,我在这里掩盖了大量的代码,但是我有一个很难解决这个问题的时间,因为它似乎非常依赖于导致它的
事件。感觉就像那种内存踩踏

我记得在我攻击C ++的时候看到的错误。 :-(


我坦率地不明白如果

在调用调用时没有传递任何值,那么terrainArgs可以有一个值,缺少一些

模糊的编译器错误(这是Python 2.4.3)。我是天真的吗?

有什么方法我可以把这个带给自己吗?


我可以通过让调用者明确地设置

terrainArgs轻松解决这个奇怪的问题,但我不能动摇这个感觉这个

" fix"只是掩盖了我的代码中的一些更深层的缺陷。



这是一个FAQ:

http://effbot.org/pyfaq/why-are- defa ... en-objects.htm


Diez


Jasper写道:


我很难过。我正在调用一个有关键字args的方法,但不是

设置他们中的其中一个从数据开始?!


类定义的开头如下:


类BattleIntentionAction(BattleAction):

def __init __(self,factionName,location,tactic =''hold'',

targetFacName ='''',terrainArgs = [],garrisonIds = []) :

self.terrainArgs = terrainArgs

print terrainArgs


构造函数在其他地方调用,如下所示:

act = BattleIntentionAction(facName,self.location)


在此对象的构造过程中,terrainArgs设置为带有

值的列表对应于之前创建的BattleIntentionAction!



默认参数值是在函数对象创建的时候评估的(通过def语句,即),而不是调用生成的

函数。如果你改变默认值,突变

将会坚持。


这在FAQ,教程和参考手册中有解释,
并希望在你最喜欢的python书中也是如此;参见例如

http:// docs。 python.org/tut/node6.htm...00000000000000
http://docs.python.org/ref/function.html



I''m stumped. I''m calling a method that has keyword args, but not
setting them, and yet one of them starts off with data?!

The class definition begins like so:

class BattleIntentionAction( BattleAction ):
def __init__( self, factionName, location, tactic=''hold'',
targetFacName='''', terrainArgs=[], garrisonIds=[] ):
self.terrainArgs = terrainArgs
print terrainArgs

The constructor is called somewhere else, like so:
act = BattleIntentionAction( facName, self.location )
During this object''s construction, terrainArgs is set to a list with
values corresponding to a previously created BattleIntentionAction!
Even more bizarre, the terrainArgs param is a testing formality, and
doesn''t actually get used anywhere in my code -- the corresponding
attribute is always modified after object creation. Furthermore, this
doesn''t happen with the other keyword args...

Obviously, I''m glossing over a ton of code here, but I''m having a
tough time isolating this problem, as it seems to be very dependent on
events leading up to it. It feels like the sort of memory stomping
bug I remember seeing from days of yore when I hacked C++. :-(
I frankly don''t understand how "terrainArgs" can have a value if
nothing is passed for it on the calling invocation, short of some
obscure compiler bug (this is Python 2.4.3). Am I being naive? Is
there some way I could be bringing this about myself?

I can easily work around this weirdness by having the caller set
terrainArgs explicitly, but I can''t shake the sensation that this
"fix" just masks some deeper flaw in my code.
Arg!
-Jasper

解决方案

2008/8/18 Jasper <ja****@peak.org>:

I''m stumped. I''m calling a method that has keyword args, but not
setting them, and yet one of them starts off with data?!

<http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects>

--
Cheers,
Simon B.
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues | Twitter: brunns


Jasper schrieb:

I''m stumped. I''m calling a method that has keyword args, but not
setting them, and yet one of them starts off with data?!

The class definition begins like so:

class BattleIntentionAction( BattleAction ):
def __init__( self, factionName, location, tactic=''hold'',
targetFacName='''', terrainArgs=[], garrisonIds=[] ):
self.terrainArgs = terrainArgs
print terrainArgs

The constructor is called somewhere else, like so:
act = BattleIntentionAction( facName, self.location )
During this object''s construction, terrainArgs is set to a list with
values corresponding to a previously created BattleIntentionAction!
Even more bizarre, the terrainArgs param is a testing formality, and
doesn''t actually get used anywhere in my code -- the corresponding
attribute is always modified after object creation. Furthermore, this
doesn''t happen with the other keyword args...

Obviously, I''m glossing over a ton of code here, but I''m having a
tough time isolating this problem, as it seems to be very dependent on
events leading up to it. It feels like the sort of memory stomping
bug I remember seeing from days of yore when I hacked C++. :-(
I frankly don''t understand how "terrainArgs" can have a value if
nothing is passed for it on the calling invocation, short of some
obscure compiler bug (this is Python 2.4.3). Am I being naive? Is
there some way I could be bringing this about myself?

I can easily work around this weirdness by having the caller set
terrainArgs explicitly, but I can''t shake the sensation that this
"fix" just masks some deeper flaw in my code.

This is a FAQ:

http://effbot.org/pyfaq/why-are-defa...en-objects.htm

Diez


Jasper wrote:

I''m stumped. I''m calling a method that has keyword args, but not
setting them, and yet one of them starts off with data?!

The class definition begins like so:

class BattleIntentionAction( BattleAction ):
def __init__( self, factionName, location, tactic=''hold'',
targetFacName='''', terrainArgs=[], garrisonIds=[] ):
self.terrainArgs = terrainArgs
print terrainArgs

The constructor is called somewhere else, like so:
act = BattleIntentionAction( facName, self.location )

During this object''s construction, terrainArgs is set to a list with
values corresponding to a previously created BattleIntentionAction!

default argument values are evaluated when the function object is
created (by the "def" statement, that is), not when the resulting
function is called. if you mutate the default values, the mutations
will stick.

this is explained in the FAQ, the tutorial, and the reference manual,
and hopefully in your favourite python book as well; see e.g.

http://docs.python.org/tut/node6.htm...00000000000000
http://docs.python.org/ref/function.html

</F>


这篇关于奇怪的方法keyword-arg bug。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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