-4

Simple python code

def m(x,y):
  x=2
  y=[3,5]

a=1
b=[0,26]
print m(a,b)

I want to change the arguments,if it is possible without return.When I run my code

python d1.py 
None

Why? With return

def m(x,y):
  x=2
  y=[3,5]
  return x,y

a=1
b=[0,26]
print m(a,b)

I got

(2, [3, 5])

What I want is a and b printed.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Sinisha
  • 107
  • 1
  • 2
  • 7
  • What do you want it to print? – Burhan Khalid Feb 02 '16 at 11:50
  • 2
    Your question, and especially the title, is very unclear, but you probably want `m()` to `return` something. – Frédéric Hamidi Feb 02 '16 at 11:50
  • Because all functions implicitly return `None` and you've printed return value from your function? – Łukasz Rogalski Feb 02 '16 at 11:51
  • As your `definition` does not `return` anything (`None`) it will print that out. – Gábor Erdős Feb 02 '16 at 11:54
  • I am confused, why are you surprised that you get `(2,[3,5])` as an output? Please describe what you want your function to do in an understandable way. – Bas Jansen Feb 02 '16 at 12:12
  • Do you want to change the value of `a` and `b` within the function? In general this is not possible, but can work around this problem thanks to mutable types (such as lists). If you explain what you are trying to accomplish, we can work towards a solution – Andrea Corbellini Feb 02 '16 at 12:13
  • To change **global** variables you [need to use `global a,b`](http://stackoverflow.com/questions/10588317/python-function-global-variables). But I do not know what you want good enough to give a useful answer... – Nander Speerstra Feb 02 '16 at 12:17
  • @AndreaCorbellini it is possible using `global` as @NanderSpeerstra states. I agree with hearing the OP out though. If we can clarify the misunderstanding, that's most of the question probably solved. – Ogaday Feb 02 '16 at 12:20
  • 2
    @Ogaday: `global` is usually a symptom of bad design. I think beginners should not be trained to use it, also because they may face unexpected behaviors. However that's just my humble opinion :) – Andrea Corbellini Feb 02 '16 at 12:24
  • @AndreaCorbellini Fair enough! I've never actually used it myself, came across it recently it and it does seem like it opens up a few potential pitfalls. – Ogaday Feb 02 '16 at 12:25
  • @AndreaCorbellini, you're right: changing global variables is almost always a bad idea. However, in this specific question it is asked how to change variables **if possible without return** (a specific demand). You're then bound to use `global`... – Nander Speerstra Feb 02 '16 at 12:28

1 Answers1

2

What I assume you want to achieve is to pass your arguments by reference, so that they get changed by the function.

You cannot achieve that passing immutable arguments (like numbers) into your function. To deal with that, take a look at the official docs. For clarity I'd strongly advise to use solution 1, though it uses a return statement:

def m(x,y):
    x = 2
    y = [3,5]
    return x,y

a = 1
b = [0,26]
a,b = m(a,b)
print a,b

Achieving this "without return" is possible using one of the other solutions given in the documentation. From those I'd say solution 3 is preferable, though it's much less clear and straightforward than solution 1. You pass a mutable argument to your function, like a list:

def m(x):
    x[0] = 2
    x[1] = [3,5]

a = [1, [0,26]]
m(a)
print a
Jan Christoph Terasa
  • 5,781
  • 24
  • 34