0

I am trying to change number inside of function with change_value but as you can see it doesn't work

def change_value(number):

    number += 1


def function():

    number = 0

    change_value(number)

    print(number)


function()

When I run it, it prints out 0 instead of 1

JJP7
  • 33
  • 1
  • 7
  • You need to learn the difference between "pass by reference" and "pass by value". The short version is that `number` in your `change_value` function is a **different** variable than the one in your `function` function. – Shadow Jul 05 '19 at 01:59
  • Read on: https://stackoverflow.com/a/986145/937153 – Unni Jul 05 '19 at 02:00
  • 1
    you should use `return number` in `change_value` and then you can do `number = change_value(number)`. This way everyone can see that you change value in variable `number`. – furas Jul 05 '19 at 02:23

4 Answers4

1

In Python, all function parameters are passed by object sharing. This means you can mutate the value (if it is mutable), but you can never swap out a value for another (i.e. change the variable's binding, so that the original variable is affected).

Numbers are immutable. There is no way for you to do this.

If you place a number inside a mutable container (such as a list, a dict, or an object designed to do it), then you can do something like this:

def change_value(number_holder):
    number_holder[0] += 1

def function():
    number_holder = [0]
    change_value(number_holder)
    print(number_holder[0])
Amadan
  • 191,408
  • 23
  • 240
  • 301
0

Here are some ways to achieve what you want

Using a mutable

def change_value(number):
    number[0] += 1
def function():
    number = [0]
    change_value(number)
    print(number[0])

Using classes

class myNumber:
    def __init__(self, n):
        self.num = n

    def __add__(self, val):
        self.num += val

    def __repr__(self):
        return str(self.num)

def change_value(number):
    number += 1


def function():
    number = myNumber(0)
    change_value(number)
    print(number)
Perplexabot
  • 1,852
  • 3
  • 19
  • 22
0

Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable in python. So, what you can do here is to return the modified variable and reassign it.

Code:

def change_value(number):
    number += 1
    return number

def function():
    number = 0
    number = change_value(number)
    print(number)

function()

output:

1

Whereas, objects of built-in types like (list, set, dict) are mutable.

code:

def change_value(number):
    number.append(1)


def function():
    number = []
    change_value(number)
    print(number[0])

function()

output:

1
Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
0

I have changed two lines in your code to get your desired result:-

def change_value(number):
    number += 1
    return number   # Returning an incremental value


def function():
    number = 0
    number = change_value(number)
    print(number)

function()

Output

1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rahul charan
  • 765
  • 7
  • 15