0

I was wondering if this is considered an assignment in Python or not

def func(a,b,c=10): return a+b+c

Is c = 10 syntax assigns 10 to the argument c here ?

4
  • What do you want to know? The parameter c has the value 10 if none is given when func is called. The = is a default parameter definition. Commented Jul 13, 2014 at 11:48
  • What do you mean by assignment? Some reasonable interpretations include "parsed as an assignment by the parser", "treated like a (conditional) assignment at the start of the function body", "makes the variable local" and each of those leads to a different answer. Commented Jul 13, 2014 at 11:48
  • I meant "parsed as an assignment by the parser" Commented Jul 13, 2014 at 12:13
  • It's an assignment in the generic sense (providing a value for a name at some point), but it's not parsed as an assignment statement as defined in the Python grammar. Commented Jul 13, 2014 at 12:39

1 Answer 1

1

Is c = 10 syntax assigns 10 to the argument c here ?

Not necessarily. If the function is called func(1,2,3) then 10 is not assigned to c. That's the whole point of a default argument value, it's used only if not specified by the caller.

Assuming that the caller doesn't specify a value, then the default is assigned to c in the same sense that the specified arguments are assigned to a and b.

However, this is not an "assignment statement", so there are facts about assignment statements that aren't true of this (probably the most important difference, although it doesn't matter in this example of 10, is that the right hand side of this isn't evaluated each time, whereas the right hand side of an assignment statement is evaluated each time it's executed). So in the context of talking about "assignments" meaning "assignment statements", it wouldn't be considered an assignment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.