0

I have tried to do send variable and change its value in function. But, I have not succesful. What I have done;

>>>a = True

>>>def f (c):
       c = False

>>>f(a)
>>>a
a = True

But I want a to be False, without using return, can I do ?

Mark Hildreth
  • 42,023
  • 11
  • 120
  • 109
  • 3
    Do you have capital `C` passed to the function and lowercase `c` inside the function in the code you tested? – jonhopkins May 15 '13 at 16:14
  • 2
    @jonhopkins even if he didn't, it still wouldn't work. – Daniel Roseman May 15 '13 at 16:16
  • @DanielRoseman I actually wasn't sure if it would, but I figured I would still point that out, because it would affect stuff later on if the poster doesn't know about case-sensitivity.. – jonhopkins May 15 '13 at 16:17
  • 1
    Short answer: it can't be done, at least for simple types as given in the example. – Mark Ransom May 15 '13 at 16:18
  • Medium-length answer: Variables in Python are references. Assignment statements *change the reference*. So when you write `c = false`, you're not changing the value of what `c` *used to* point to; you're changing *what c points to*. You are *not* changing what `a` points to. (I recommend reading the top answer to the "How do I pass a variable by reference" question, though.) – Kyle Strand May 15 '13 at 16:21

0 Answers0