The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
I figured I'd check out Python today, and so far, everything is really easy, but I've run into a really strange issue. I come from a more obviously OO background (java/c#), and I'm not sure what's causing this.
The addAttack method of Character is somehow adding the attack to all the instantiations of Character. The code, when run, prints out both characters as having both Slash and Scissorlock. Aside from changing the way Abilities.Attack are created, I'm not sure why this is happening.
KnownAttacks = [] needs to go in __init__. Outside it's being treated as a member of the class, not of the instances. You can see that if you print Character.KnownAttacks (which shouldn't exist).
So should HP and AP, really. You'd get the same behavior if they were objects.
To elaborate a bit since you're new to Python: In Python, everything is an object. This includes classes, and even functions. So in addition to creating new instances with Character(), you can actually address Character itself, or even Character.addAttack. __init__ spawns a new instance of the class, and then returns it automatically. However it won't automatically instantiate every property of the class. You were probably misled by the (seemingly) proper behavior of HP. You don't usually need to think about references in Python, but they're there under the surface. Since HP is an integer, each instance got its own, however they were sharing the same reference to Character's KnownAttacks. You'd notice you also get "proper" behavior by changing addAttack to self.KnownAttacks = [p_attack], which would assign each instance its own unique list.
Okay, that makes sense. (Edit: and that solves the problem, thanks!) But how come pcs[1].modHP(-1) works properly? When I print out the status of the characters, Aescher has 10 HP and Cainas 9. Wouldn't they both have 9 by this logic?
Posts
So should HP and AP, really. You'd get the same behavior if they were objects.
To elaborate a bit since you're new to Python: In Python, everything is an object. This includes classes, and even functions. So in addition to creating new instances with Character(), you can actually address Character itself, or even Character.addAttack. __init__ spawns a new instance of the class, and then returns it automatically. However it won't automatically instantiate every property of the class. You were probably misled by the (seemingly) proper behavior of HP. You don't usually need to think about references in Python, but they're there under the surface. Since HP is an integer, each instance got its own, however they were sharing the same reference to Character's KnownAttacks. You'd notice you also get "proper" behavior by changing addAttack to self.KnownAttacks = [p_attack], which would assign each instance its own unique list.