How does one write a function that increments a number?

sun.aries at gmail.com sun.aries at gmail.com
Sat Jun 25 01:23:06 EDT 2005


Hi, please refer to the sections about the augments passing in Python
tutorial. Python’s pass-by-assignment scheme isn’t the same as
C++’s reference parameters, but it turns out to be very similar to
C’s arguments in practice:
	Immutable arguments act like C's "by value" mode. Objects such as
integers and strings are passed by object reference (assignment), but
since you can't change immutable objects in place anyhow, the effect is
much like making a copy.
	Mutable arguments act like C's "by pointer" mode. Objects such as
lists and dictionaries are passed by object reference, which is similar
to the way C passes arrays as pointers—mutable objects can be changed
in place in the function, much like C arrays.

Let’s have a try:
>>> def incr(counters):
	counters[0] += 1

	
>>> counters =[100]
>>> incr(counters)
>>> print counters
[101]
>>>




More information about the Python-list mailing list