In part 4 (Python in CIV) of the python guide on the modiki, I think there is an error. It says that if you use
then every time you say "Damage" afterwards it will make the entire long function call.
As far as I can tell, this is not true. If you were to run this assignment statement, then change the damage of the unit, then check the value of Damage, it would not have updated. This is because the variable damage is storing the return value of the function (OK, if you want to get technical, it's storing a reference to the return value of the function.) If you wanted to make Damage a reference to the function, you could write this:
Notice that there are no parentheses after getDamage. To use it you would write "Damage()" which would actually call the method and return the value.
Here is what I did in a python console to verify this:
Of course this is a wiki so anyone can go in and fix this. But I didn't want to make important changes to someone else's guide without getting feedback on it.
Thanks,
Daniel
Code:
Damage = CyGlobalContext().getPlayer(1).getUnit(0).getDamage()
As far as I can tell, this is not true. If you were to run this assignment statement, then change the damage of the unit, then check the value of Damage, it would not have updated. This is because the variable damage is storing the return value of the function (OK, if you want to get technical, it's storing a reference to the return value of the function.) If you wanted to make Damage a reference to the function, you could write this:
Code:
Damage = CyGlobalContext().getPlayer(1).getUnit(0).getDamage
Here is what I did in a python console to verify this:
Code:
>>> class test: def __init__(self): self.x = 0 def getX(self): return self.x >>> t = test() >>> t.x 0 >>> t.getX() 0 >>> x = t.getX() >>> x 0 >>> t.x = 1 >>> x 0 >>> x = t.getX >>> x> >>> x() 1 >>> t.x = 2 >>> x() 2
Thanks,
Daniel
Comment