[EMAIL PROTECTED] writes: > Mike Meyer wrote: >> [EMAIL PROTECTED] writes: >> > Mike Meyer wrote: >> >> "ex_ottoyuhr" <[EMAIL PROTECTED]> writes: >> >> > I'm trying to create a function that can take arguments, say, foo and >> >> > bar, and modify the original copies of foo and bar as well as its local >> >> > versions -- the equivalent of C++ funct(&foo, &bar). >> >> C++'s '&' causes an argument to be passed by reference. Python does >> >> that with all arguments. Any changes you make to the argument in the >> >> function will be seen in the caller unless you explicitly make a copy >> >> to pass. >> > except when foo and bar are bound to immutable objects. >> Wrong. >> > In C: >> > >> > int foo=1; >> > int bar=2; >> > >> > void update(int *a, int *b) { *a=3; *b=4} >> > >> > update(&foo, &bar); >> >> Note that this update is using an assignment statement, and thus >> changing the arguments. > void update(int a, int b) { a=3; b=4} > Is this also an assignment statement ?
Yes. But C calls by value, and passes copies of the original a and b in. So in this case, you change the copies. >> > In Python: >> > >> > foo=1 >> > bar=2 >> > >> > def update(a,b): a=3; b=4 >> > >> > update(foo,bar) >> This update isn't changing the objects, it's rebinding the names in >> the local name space. Since you didn't change the objects, there's no >> change to see in the calling environment. >> > Many people from C/C++ background would be tricked for this situation. >> That's because they don't understand binding. Any language that has >> bindings instead of has assignments will "trick" them this way. > Sure, any language that behave like this would trick them, I am not > saying python is the only one or that there is anything wrong with this > behaviour. I was just saying that it is a situation that they get > tricked, because of their "perception" about "=", especially when they > are told that any change to the arguments are seen by the caller. Except "trick" is a poor word choice. Nobody is playing a trick on them - they just don't understand what is going on. <mike -- Mike Meyer <[EMAIL PROTECTED]> http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list