-1

New to Python and was testing simple example of passing by reference versus passing by value. But I ran into an unexpected syntax hurdle, but not sure what I'm missing here.

#!usr/local/bin python3

def print_reference(list[:]):
    temp = list.pop()
    print(temp)

def print_value(list):
    temp = list.pop()
    print(temp)

list = ['hello', 'goodbye', 'laters']

print_reference(list)
print("After Reference" + list)
print_value(list)
print("After Value" + list)
kwalsh
  • 3
  • 4
  • Forgot to mention, syntax error is "list[:]" argument – kwalsh Mar 04 '18 at 22:14
  • 3
    That doesn't make sense as a parameter. You'd use that to make a shallow copy *when you call the function*, not when you define it. Also don't name your own lists `list`. – jonrsharpe Mar 04 '18 at 22:15
  • 1
    This isn't C++. Python parameters are always passed the same way. See https://nedbatchelder.com/text/names.html – user2357112 Mar 04 '18 at 22:16
  • It would be to preserve "list", if I wanted to use its contents, but not modify its contents. – kwalsh Mar 04 '18 at 22:17
  • @user2357112, that's true, C/C++ is my background, I'll take a look – kwalsh Mar 04 '18 at 22:18
  • Then do that outside or inside the function, but you can't do it in the parameter definition. It's simply not valid syntax, as the error tells you. – jonrsharpe Mar 04 '18 at 22:18

4 Answers4

2

I think you got the names backwards. Copy inside the function print_value with lst = lst[:]

def print_reference(lst):
    temp = lst.pop()
    print(temp)

def print_value(lst):
    lst = lst[:]
    temp = lst.pop()
    print(temp)

lst = ['hello', 'goodbye', 'laters']

print_value(lst)
print("After Value", lst)
print_reference(lst)
print("After Reference", lst)

Output:

laters
After Value ['hello', 'goodbye', 'laters']
laters
After Reference ['hello', 'goodbye']

BTW, list is not good variable name because it shadows the built in 'list`.

Important is to distinguish what happens a compile time and what at run time. This is fixed once the functions is defined:

def print_value(lst):

Therefore, copying list with [:] doesn't make sense and you get a syntax error.

But everything inside the function is executed when the function is a called. Therefore, this line inside the function:

lst = lst[:]

makes a new (shallow) copy of lst every time the function is called. This copy lives only inside the function and will be garbage collected once the function is finished (returned).

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
0

I think this is what you are trying to do maybe xD. Welcome to python anyways!

def print_reference(list):
    list = list[:]
    temp = list.pop()
    print(temp)

def print_value(list):
    temp = list.pop()
    print(temp)

list = ['hello', 'goodbye', 'laters']

print_reference(list)
print("After Reference",list)
print_value(list)
print("After Value",list)

The issue is that you have to pass a parameter without editing it. You can edit it inside the function but not directly inside the brackets.

def print_reference(list):

It is probably a good idea to change the list name to something like l or lst instead of list as the list() function is used to make objects like strings, into a list.

Crawley
  • 600
  • 2
  • 8
  • 15
0
def print_reference(lst): # You cannot do a list slice as an argument
    l = lst[:]
    temp = l.pop()
    print(temp)

def print_value(lst):
    temp = lst.pop()
    print(temp)

lst = ['hello', 'goodbye', 'laters']

print_reference(lst)
print("After Reference: " + ''.join(lst))
print_value(lst)
print("After Value: " + ''.join(lst))
adapap
  • 665
  • 1
  • 9
  • 21
0

Because it is syntax error. I do not understand this def print_reference(list[:]): In second it is OK, you put parameter of name list to function. You can specify type by def print_reference(name: list): see this :How do I pass a variable by reference?

Lukasz
  • 152
  • 3
  • 16