Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7.2 Assignment statements documentation is vague and slightly misleading #70052

Open
abarnert mannequin opened this issue Dec 15, 2015 · 3 comments
Open

7.2 Assignment statements documentation is vague and slightly misleading #70052

abarnert mannequin opened this issue Dec 15, 2015 · 3 comments
Labels
docs Documentation in the Doc dir

Comments

@abarnert
Copy link
Mannequin

abarnert mannequin commented Dec 15, 2015

BPO 25865
Nosy @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2015-12-15.07:47:24.467>
labels = ['docs']
title = '7.2 Assignment statements documentation is vague and slightly misleading'
updated_at = <Date 2015-12-15.10:44:55.666>
user = 'https://bugs.python.org/abarnert'

bugs.python.org fields:

activity = <Date 2015-12-15.10:44:55.666>
actor = 'martin.panter'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2015-12-15.07:47:24.467>
creator = 'abarnert'
dependencies = []
files = []
hgrepos = []
issue_num = 25865
keywords = []
message_count = 3.0
messages = ['256443', '256444', '256454']
nosy_count = 3.0
nosy_names = ['docs@python', 'martin.panter', 'abarnert']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue25865'
versions = []

@abarnert
Copy link
Mannequin Author

abarnert mannequin commented Dec 15, 2015

From https://docs.python.org/3/reference/simple_stmts.html#assignment-statements

If the target list contains one target prefixed with an asterisk, called a “starred” target: The object must be a sequence with at least as many items as there are targets in the target list, minus one. The first items of the sequence are assigned, from left to right, to the targets before the starred target. The final items of the sequence are assigned to the targets after the starred target. A list of the remaining items in the sequence is then assigned to the starred target (the list can be empty).
Else: The object must be a sequence with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

The word "sequence" is used here multiple times. But the object being assigned to the target list can be any iterable (including an iterator, a set, some weird custom iterable you invent, ...).

If the target is a target list enclosed in parentheses or in square brackets: The object must be an iterable with the same number of items as there are targets in the target list, and its items are assigned, from left to right, to the corresponding targets.

This doesn't mention that you can have a starred target inside the parenthesized or bracketed target list. More importantly, it covers the exact same case already covered above. There's semantics for "assignment of an object to a target list, optionally enclosed in parentheses or square brackets", and then semantics for "Assignment of an object to a single target... If the target is a target list enclosed in parentheses or in square brackets".

According to the grammar, a single target (no commas, parens, or brackets) is a target list, but according to the previous paragraphs it seems like it isn't.

Finally, there's nothing that explains what distinguishes between target list assignment and single target assignment. As far as I can tell, it's treated as a target list iff there's a comma, or there's square brackets around it (similar to the fact that tuples are defined by commas, but lists by square brackets--but without the exception for ()); that should probably be documented.

There's also some needless repetition there.

I think better wording might be something like this:

Assignment is recursively defined as follows.

  • If the target list is a comma-separated list of two or more targets, optionally enclosed in parentheses, or a comma-separated list of zero or more targets enclosed in square brackets:

(It might be worth having a note here pointing out that these are almost the same rules for non-comprehension tuple and list displays, except that () is a tuple but not a target list. Or would that just be confusing?)

(It might also be worth mentioning that this is the same feature referred to as "iterable unpacking", "sequence unpacking", and/or "tuple unpacking" in PEP-3132, the official tutorial, and lots of non-official materials?)

** If the target list contains one target prefixed with an asterisk, called a “starred” target: The object must be an iterable with at least as many items as there are targets in the target list, minus one. The first items of the iterable are assigned, from left to right, to the targets before the starred target. The final items of the iterable are assigned to the targets after the starred target. A list of the remaining items in the iterable is then assigned to the starred target (the list can be empty).

** Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

  • Otherwise, the target list is treated as a single target.

** If the target is an identifier (name): ...

(No section on bracketed target list here; that's already covered above.)

** If the target is an attribute reference: ...

** ...

@abarnert abarnert mannequin assigned docspython Dec 15, 2015
@abarnert abarnert mannequin added the docs Documentation in the Doc dir label Dec 15, 2015
@abarnert
Copy link
Mannequin Author

abarnert mannequin commented Dec 15, 2015

As a side note, why isn't () allowed as an empty target list, like []? Then the rules for target lists vs. single targets would be exactly parallel to the rules for tuple and list displays. And the error message can't assign to () seems a bit weird--you can't really assign to the tuple a, b, c or (a, b, c), but that's not what you're doing; you're just specifying a target list.

@vadmium
Copy link
Member

vadmium commented Dec 15, 2015

Regarding assigning to (), see bpo-23275. Looks like there is a patch to enable it.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

1 participant