1

Like an example

def inc(a):
    a += 1

If I want to have an increment function instead or writing var += 1 (Is not the only case, just as example) what I should do? I know that I can return a + 1 but I want void fucntion. Is there any ways in python?

sashaaero
  • 2,618
  • 5
  • 23
  • 41
  • Are you asking for an in place increment function? – Paul Rooney Feb 01 '16 at 05:19
  • @PaulRooney yes. With no OOP. – sashaaero Feb 01 '16 at 05:20
  • FWIW, there are no `void` functions in Python. If you don't provide an explicit return value (i.e., the function exits without a `return` statement, or it uses `return` without a value) the function will return `None`. – PM 2Ring Feb 01 '16 at 05:23
  • @PM2Ring thanks. I know that but I was thinking about another langs while asking, sorry. – sashaaero Feb 01 '16 at 05:25
  • I think what you want is to pass the variable as a reference. check this one: http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – realhu Feb 01 '16 at 05:32
  • @realhu that doesn't work with `immutable types` like `int` so I don't think that would help here. But yes that is exactly what @Aero is trying to do. – Tadhg McDonald-Jensen Feb 01 '16 at 05:38
  • @TadhgMcDonald-Jensen Yes, It is true we can't do it with int, but we can create a class and overwrite the magic class. Please check my answer:-) – realhu Feb 01 '16 at 07:03

4 Answers4

3

You can do this by making a global

def add_one():
    global a
    a += 1

Notice you don't have to pass a into the function. I would highly recommend against doing this, however.

John Howard
  • 61,037
  • 23
  • 50
  • 66
3

The Python data model doesn't really have variables like other languages. It has objects which may be bound to names. So a Python "variable" isn't a memory location like it is in many other languages, it's simply a label on an object. An object may have multiple names, or it may have none. See Facts and myths about Python names and values by SO veteran Ned Batchelder for further information on this topic.

Python integers are immutable objects, so you can't actually increment them. Of course, you can create a new integer object that has a value 1 greater than the object currently named a and bind the name a to that new object.

So what you're asking to do isn't exactly a natural operation in Python. However, you can get close. As others have mentioned, you can sort-of do it by placing a into a mutable container object. Eg,

def inc(lst):
    lst[0] += 1

a = 7
b = [a]
inc(b)
print b, a

output

[8] 7

A somewhat more satisfactory approach is to refer to the name via the global() dictionary:

def inc(k):
    globals()[k] += 1 

a = 7
inc('a')
print a

output

8

However, modifying things via globals() is generally frowned upon, and it's useless if you want to modify a name that's local to a function.


Another option is to define a custom class:

class MutInt(object):
    def __init__(self, v):
        self.v = v

    def __str__(self):
        return str(self.v)

    def inc(self):
        self.v += 1

a = MutInt(7)
print a
a.inc()
print a

output

7
8

But that's still rather ugly (IMHO), and you'd have to define all the methods of int in it to make the class useful.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0

You need the global statement to modify a global variable in python:

def inc(amount=1):
    global a
    a+=amount

a = 1
inc(2)
print(a)

this will allow the function to override the value of the globally defined variable.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
0

We can't pass immutable type like int as a variable as a reference:How do I pass a variable by reference. But we can create a new class and overwrite this one's self add operation with python's magic class __iadd__.

class CInt(object):

    x = 0

    def __init__(self, x):
        self.x = x

    def __add__(self, y):
        return CInt(self.x + y)

    def __iadd__(self, y):
        self.x = self.x + y


def inc(a):
    a += 1


a = CInt(2)

print(a.x)

inc(a)

print(a.x)

The result would be:

2
3
Community
  • 1
  • 1
realhu
  • 935
  • 9
  • 12