sashaaero
sashaaero

Reputation: 2908

Python procedure for changing an object

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?

Upvotes: 1

Views: 84

Answers (4)

realhu
realhu

Reputation: 985

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

Upvotes: 0

PM 2Ring
PM 2Ring

Reputation: 55489

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.

Upvotes: 3

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21453

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.

Upvotes: 0

John Howard
John Howard

Reputation: 64205

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.

Upvotes: 3

Related Questions