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

3
  • 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. Commented Jul 5, 2019 at 1:59
  • Read on: stackoverflow.com/a/986145/937153 Commented Jul 5, 2019 at 2: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. Commented Jul 5, 2019 at 2:23

4 Answers 4

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])
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.