How does one write a function that increments a number?

A

anonymousnerd

Apologies if this question seems stupid: How does one write a
function that increments a value in Python? When I tried, the variable
never changed.
The session went like this: counter = int(counter)
counter += 1

Thanks in advance,
Vaibhav
 
S

sun.aries

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: counters[0] += 1

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

anonymousnerd

Wait... so this means it is impossible to write a function that
increments an integer without turning the integer into a list?
 
G

George Sakkis

Wait... so this means it is impossible to write a function that
increments an integer without turning the integer into a list?

The short answer is no you can't, because integers are immutable (as
well as floats and strings among others). The longer answer is you can
create a, say, MutableInt class whose instances behave as modifiable
integers. Most probably you don't really need this, but if you think
you do, others in the list will sketch out how.

George
 
B

Brian van den Broek

(e-mail address removed) said unto the world upon 25/06/2005 01:41:
Wait... so this means it is impossible to write a function that
increments an integer without turning the integer into a list?

Well, one of these options will probably suit:
.... data += 1
.... return data
....1

Or, if you only care about one counter, don't like the
return/assignment form, and don't mind all the cool kids frowning on
the use of global:

.... global counter
.... counter += 1
....

Nicest might be using a class, where you keep a clean namespace, and
don't have the return/assignment form:
.... def __init__(self):
.... self.counter = 0
.... def increment_counter(self):
.... self.counter += 1
....
This also lets you have multiple independent counters:

Best,

Brian vdB
 
S

Steven Bethard

Apologies if this question seems stupid: How does one write a
function that increments a value in Python? When I tried, the variable
never changed.
The session went like this:

counter = int(counter)
counter += 1

1

You probably don't really want to write code like this in Python. How
about:

class Counter(object):
def __init__(self, start=0):
self.value = start
def incr(self):
self.value += 1

counter = Counter(1)
counter.incr()
print counter.value

Or you can likely get everything you need from itertools.count.

STeVe
 
S

Steven D'Aprano

Apologies if this question seems stupid: How does one write a
function that increments a value in Python? When I tried, the variable
never changed.
The session went like this:
counter = int(counter)
counter += 1

1

Why does it need to be a function? Why complicate matters? Isn't it
simpler to just do this:

counter = 1
counter += 1
print counter
 
M

Mandus

Sun, 26 Jun 2005 04:14:19 +1000 skrev Steven D'Aprano:
Why does it need to be a function? Why complicate matters? Isn't it
simpler to just do this:
[snip]

I guess he have some reason for it...

it's because python do call by value, not by reference for such simple
variables.

If you absolutely want to do this, you can try:

def incr(counter):
counter[0] += 1

counter=[1]
incr(counter)
print counter[0]

But I agree with Steven, this is probably not what you want to do :)
 
D

Dennis Lee Bieber

Wait... so this means it is impossible to write a function that
increments an integer without turning the integer into a list?

In older languages, a function is defined as subprogram which
returns a value on invocation (and side-effects -- that is, changes in
the values of input arguments -- are frowned upon). A
subroutine/procedure, OTOH, does not return a value and side-effects are
expected.

The simplest way to look at Python is that it is all "functions"
and one should not expect to change arguments (I'm ignoring the matter
of lists and dictionaries, where one is changing the contents of the
list/dictionary, but not the dictionary itself, per se).

So the full definition of your increment requires the usage...

def incr(x):
return x + 1

a = 4
a = incr(a)

Python "def"s that don't have an explicit "return" still return
a value -- "None"
.... x = x + 1
.... .... return x + 1
....
 
D

Dennis Lee Bieber

it's because python do call by value, not by reference for such simple
variables.
Not correct...

Python passes the reference to the argument... But the
assignment inside the function, as with all Python assignments, defines
a new association -- the argument "name" is now pointing to some /other/
object.
.... print id(x)
.... x = x + 1
.... print id(x)
....
3307128
3307116

Note how the first id(x) has the same integer object as 'a'...
but after the assignment, id(x) is something else.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top