1

In Python, obviously it's possible to assign to names, e.g. a = 1. It's also valid to assign to attributes (a.b = 1 - AFAIK this corresponds to the __setattr__ special method) and indexing operations (a[b] = 1, which corresponds to __setitem__).

In Lua, those are the only valid assignment targets - the language's grammar enforces that restriction: var ::= Name | prefixexp '[' exp ']' | prefixexp '.' Name.

Are those three forms also an exhaustive list of valid assignment targets in Python or are there others? (For example, according to this answer, it's not valid to assign to a function call: f() = 1 is invalid.)

Also, is the restriction to valid assignment targets enforced by the grammar as in Lua, or later in the source => bytecode compilation process?

3
  • @metatoaster - Thanks! It looks like the PEG grammar does indeed specify this precisely (whereas the old CFG-based grammar didn't). Valid assignment targets are the three forms I mentioned (the PEG's single_target) and the unpacking construct mentioned by @blhsing (star_targets). Commented Sep 2, 2024 at 5:13
  • Pls explain lua syntax var ::= Name | prefixexp '[' exp ']' | prefixexp '.' Name briefly as I dont know lua Commented Sep 2, 2024 at 6:51
  • @SuramuthuR - It's a form of EBNF grammar - it says that a var is either a Name, an expression ending in square brackets, or an expression ending in . followed by a Name. Comma-separated lists of those vars are the only things that can appear to the left of = in Lua. Commented Sep 2, 2024 at 9:56

0

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.