[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Python Question



Geekius Maximus wrote:

I have a general Python language question. I know this is the pygame board, but I don't know who else to ask. I'm trying to alter an argument rect in a method - change it from a Rect to None, but the change doesn't effect the variable in the calling function. What do I do?


Probably the best place to ask a generic Python question is on the comp.lang.python newsgroup,
which is also the python-list@xxxxxxxxxx mailing list (I'm pretty sure you don't have to be signed
up to post). The list is quite high-volume, and it's fairly typical usenet fair, so keep your shields
up, but you can usually get help on this kind of stuff.


I think you've already received your answer -- functions are "call by object reference" in
Python. You get the reference to the original, reassigning that value does not affect
other references to the original object.


If you want to change something that is passed to you, you can't reassign it. What you
can do is mutate it, IF it is mutable (mutable objects include lists, dictionaries, and
class instances).


So, for example, if I have a control structure modelled as an object:

class settings(object):
   spam_quotient = 2
   exploding_scotsmen_count = 14

S = settings()

I can write a function which mutates this:

def test_scotsman(S):
   S.exploding_scotsmen_count -= 1
   if S.exploding_scotsmen_count < 0:
      print "All out of exploding scotsmen."

Cheers,
Terry


-- Terry Hancock (hancock@xxxxxxxxxxxxxxxxxxxx) Anansi Spaceworks http://www.AnansiSpaceworks.com