0

I am currently learning python and got stuck at one of the program. Here is the snippet

test_board = ['#','X','O','X','O','X','O','X','O','X']
print(test_board)
def place_marker(board, marker, position):
    board[position] = marker

place_marker(test_board, '$', 8)
print(test_board)

The output of above program is

['#', 'X', 'O', 'X', 'O', 'X', 'O', 'X', 'O', 'X']
['#', 'X', 'O', 'X', 'O', 'X', 'O', 'X', '$', 'X']

I am confused that how did the value of variable test_board got updated ? I didn't set the global variable nor I am returning the value. Can someone please help me understand ?

3
  • 2
    In python, list are mutable object Commented Feb 22, 2019 at 4:52
  • 1
    When you pass a parameter to a function, you get a reference to the object that was given by the caller. If that object is mutable you can change it as you did with board[position] = .... Now that you know that, the links given at the top should make more sense. Commented Feb 22, 2019 at 4:56
  • Yes. Thank you so much. Now I can proceed. Commented Feb 22, 2019 at 4:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.