Bizarre method keyword-arg bug.

Diez B. Roggisch deets at nospam.web.de
Mon Aug 18 04:52:02 EDT 2008


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-default-values-shared-between-objects.htm

Diez



More information about the Python-list mailing list