-1

How can I get this function:

def test(var1):
    var1=5
a=0
test(a)
print(a)

To set variable a to equal 5.

1
  • @Aशwiniचhaudhary Aah, I was looking for that only, but couldn't get it... :( Commented Apr 30, 2014 at 5:18

3 Answers 3

1

You'll have to use a mutable variable, for example a list. This does something similar.

def test(var1):
    var1[0]=5
a=[0]
test(a)
print(a[0])
Sign up to request clarification or add additional context in comments.

Comments

0

You probably want to return the value:

def test(var1):
    var1 = 5
    return var1
a = 0
a = test(a)
print(a)

Comments

0

Yes..Brad is correct...But a small correction there. We should say it as mutable object. When you use a mutable object like List, Dictionary you will achieve this..

>>> def test(var1):
...  var1.append(5)
 ...
>>> a=[]
>>> test(a)
>>> a
[5]

whereas, When you use immutable object like integer, string, tuple it wont change the value referenced by the object.

>>> def test(val1):
...  val1+(1,)
...
>>> test(a)
>>> a
()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.