Johan Falkenjack
Johan Falkenjack

Reputation: 199

Definition of "operator", and by extension "operand", in Python terminology

Is this quote correct?

fot += [duk] is an assignment in Python, actually a shortening of fot = fot + [duk]
fot and [duk] are not operands in fot += [duk]
however, they are operands in fot + [duk]

Background:

I'm currently substituting as lecturer in an undergraduate course in introductory programming using Python as teaching language. I was going through some materials covering a teaching exercise about programming terminology that they've been doing in this course the last few years. The quote above comes from a list of common mistakes that the students have made during the exercise previous years. I've translated it from the original Swedish and added emphasis and formatting.

In Python += is called one of the augmented assignment operators and in my mind, if something is an operator, whatever it operates on is an operand. I.e. fot and [duk] above are operands of the += operator. However, it's been 15 years since I took a course on programming languages from a more formal point of view, so I might have forgotten a lot of important details that matters in this case. Mainly, I'm thinking that something about assignment being a statement might affect how we define operators and operands, but I'm basically guessing in that regard.

My research thus far

I started digging through the Python Language Reference and I think that the reason for the quote above might be found in Section 2.5 which covers lexical analysis and lists the set of tokens defined as operators in that context. That list does not include += (nor the regular assignment operator =). Instead they are listed in Section 2.6 which covers delimiters. (However, this section does refer to += as an "augmented assignment operator" so it's already getting muddy.)

Further, in Section 7.2, which covers assignment statements, = is implicitly referred to as "the assignment operator". A close reading of [Section 7.2.1], which specifically covers augmented assignment, contains the following quote [emphasis mine]:

An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once.

That quote seems to strengthen my point. It explicitly uses the word operands in the context of +=. However, it could be interpreted as the two tokens being operands of "the binary operation specific to the type of assignment" not as being operands of the augmented assignment itself. That might even be what the third line of the original quote is saying.

Coincidentally, further reading of Section 7.2.1 reveals that, regardless of lines 2 and 3, the first line from the quote above isn't quite right.

Unlike normal assignments, augmented assignments evaluate the left-hand side before evaluating the right-hand side.

I.e. fot += [duk] is not simply a macro that is expanded into fot = fot + [duk], it actually has slightly different semantics.

One guess is that whoever wrote the original quote above only looked at Section 2.5 and noted that += wasn't in the list of operators in the context of tokenization, and/or remembered that = is usually not considered an operator in math, and stopped there. However, it took me looking through multiple sections and searching for references to "operator" and "operand" to come this far and I'm still not certain. I most definitely did not read the whole Python Language Reference, though. There might be something else that affects the status of fot and [duk] as operands or not in the context of fot += [duk].

Your thoughts?

Upvotes: 0

Views: 130

Answers (0)

Related Questions