0

Good day, brilliant Stack Overflow Community. I noticed an interesting behavior regarding Python function when input as a dictionary.

When we use the empty list as a default argument, we can return that list while appending some value.

def func_list(value,input=[]):
    return input.append(value)

I would expect the same thing to apply when empty dictionary as an argument. such as:

def func_dict(value,input={}):
    return input[value] = value

However, Python will raise a Syntax Error, and I can only do something like:

def func_dict(value,input={}):
    input[value] = value
    return input

I am wondering why? Thank you so much!

PS. Please feel free to modify the question, and apologize if my expression of the question is not clear enough!

Vae Jiang
  • 337
  • 3
  • 8
  • You can't use "=" in return. The list will also raise an exception in this case: def func_list(value,input=[]): return input[0] = value – Yoav Sheetrit Dec 13 '21 at 10:12
  • Thank you so much for your answer! Indeed input[value] = xxx in this case is a statement. and Python cannot return a statement. like @vaizki suggested! – Vae Jiang Dec 13 '21 at 10:34
  • As a side note, Python now has assignment expressions via `:=` "walrus" operator. So you can turn many assignment statements into expressions but there is no walrus assignment syntax for `dict` (at least not yet). – vaizki Dec 13 '21 at 10:39

2 Answers2

1

The key difference is that input.append(value) is an expression and input[value] = value is not (it is just a statement). The return statement can only take an expression as a parameter (or no parameters to return None).

You CAN do this:

def func_dict(value,input={}):
    return input.update({ value: value }) or input

How this works is that dict.update() returns None so we return the (now updated) input instead. I know it's ugly but can't think of a better one.

vaizki
  • 1,678
  • 1
  • 9
  • 12
  • Thank you so much for your great answer! it immediately clarified my confusion! – Vae Jiang Dec 13 '21 at 10:31
  • For people who stumble upon this post in the future, here is a great summary of what is the difference between a statement and an expression like @vaizki mentioned.https://stackoverflow.com/questions/4728073/what-is-the-difference-between-an-expression-and-a-statement-in-python – Vae Jiang Dec 13 '21 at 10:43
  • I edited my answer to reflect the fact that expressions are also statements but return needs an expression. – vaizki Dec 13 '21 at 10:51
0

On another note, the word input is a protected word in python and is used for interactions with the user, you may want to use another parametername as you override the default function when using it the way you do right now.

Here's the documentation regarding input: https://docs.python.org/3/library/functions.html#input

Albin Sidås
  • 321
  • 2
  • 10
  • Ahh! I see. You are right, input indeed is a bad argument name to use! Thank you so much for pointing out my error! – Vae Jiang Dec 13 '21 at 10:33