0
def function(x):
    x = 4
variable = 0
function(variable)
print(variable)

This would output 0 but is there a way that it outputs 4? And also it should be without return.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Philipp
  • 93
  • 8

1 Answers1

1

First of all, I'd suggest you take a look to this nice explanation about python names and values. Now, one possible way to achieve what you want would be using a mutable structure as a dictionary, so you can pass your variable inside, something similar to this would do it:

def function(dct):
    dct['variable'] = 4

dct = {
    'variable': 0
}

function(dct)
print(dct['variable'])

More info can be found in python docs

BPL
  • 9,632
  • 9
  • 59
  • 117