Skip to content

Update design FAQ about assignment expression #79847

Closed

Description

@carlbordum
mannequin
BPO 35666
Nosy @serhiy-storchaka, @emilyemorehouse, @tirkarthi, @carlbordum
Dependencies
  • bpo-35224: PEP 572: Assignment Expressions
  • Files
  • design.rst: design.rst with updated faq about assignment expression
  • 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 = <Date 2019-11-12.12:46:57.090>
    created_at = <Date 2019-01-05.16:08:00.595>
    labels = ['type-feature', '3.8', 'docs']
    title = 'Update design FAQ about assignment expression'
    updated_at = <Date 2019-11-12.12:46:57.089>
    user = 'https://github.com/carlbordum'

    bugs.python.org fields:

    activity = <Date 2019-11-12.12:46:57.089>
    actor = 'xtreak'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-11-12.12:46:57.090>
    closer = 'xtreak'
    components = ['Documentation']
    creation = <Date 2019-01-05.16:08:00.595>
    creator = 'carlbordum'
    dependencies = ['35224']
    files = ['48025']
    hgrepos = []
    issue_num = 35666
    keywords = []
    message_count = 5.0
    messages = ['333063', '333065', '334343', '334344', '356451']
    nosy_count = 5.0
    nosy_names = ['docs@python', 'serhiy.storchaka', 'emilyemorehouse', 'xtreak', 'carlbordum']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue35666'
    versions = ['Python 3.8']

    Activity

    1. carlbordum commented on Jan 5, 2019

      carlbordummannequin
      MannequinAuthor

      Hi there,

      In [Doc/faq/design.rst](https://github.com/python/cpython/blob/main/Doc/faq/design.rst) there is an explanation of why Python does not have assignment in expressions. This is dated since PEP-572 / Python 3.8.

      Online version: https://docs.python.org/3/faq/design.html#why-can-t-i-use-an-assignment-in-an-expression

      I suggest updating it to the attached file. git diff:

      diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
      index e2d63a0323..e61284611d 100644
      --- a/Doc/faq/design.rst
      +++ b/Doc/faq/design.rst
      @@ -149,7 +149,15 @@ to tell Python which namespace to use.
       Why can't I use an assignment in an expression?
       -----------------------------------------------
       
      -Many people used to C or Perl complain that they want to use this C idiom:
      +In Python 3.8 and newer, you can use assignment in an expression with the
      +``:=`` operator (as described in :pep:`572`)::
      +
      +    while line := f.readline():
      +        ...  # do something with line
      +
      +For more than 25 years it was not possible to do assignments in expressions in
      +Python. Naturally, many people used to C or Perl would complain that they want
      +to use this C idiom:
       
       .. code-block:: c
       
      @@ -157,7 +165,7 @@ Many people used to C or Perl complain that they want to use this C idiom:
              // do something with line
          }
       
      -where in Python you're forced to write this::
      +where in Python you would be forced to write this::
       
          while True:
              line = f.readline()
      @@ -165,8 +173,10 @@ where in Python you're forced to write this::
                  break
              ...  # do something with line
       
      -The reason for not allowing assignment in Python expressions is a common,
      -hard-to-find bug in those other languages, caused by this construct:
      +The reason different operators are used for assignment and assignment in
      +expressions (``=`` and ``:=``, respectively), and why Python didn't allow
      +assignment in expressions for a long time is a common, hard-to-find bug in
      +those other languages, caused by this construct:
       
       .. code-block:: c
       
      @@ -180,11 +190,6 @@ hard-to-find bug in those other languages, caused by this construct:
       The error is a simple typo: ``x = 0``, which assigns 0 to the variable ``x``,
       was written while the comparison ``x == 0`` is certainly what was intended.
       
      -Many alternatives have been proposed.  Most are hacks that save some typing but
      -use arbitrary or cryptic syntax or keywords, and fail the simple criterion for
      -language change proposals: it should intuitively suggest the proper meaning to a
      -human reader who has not yet been introduced to the construct.
      -
       An interesting phenomenon is that most experienced Python programmers recognize
       the ``while True`` idiom and don't seem to be missing the assignment in
       expression construct much; it's only newcomers who express a strong desire to
      
      
    2. added
      docsDocumentation in the Doc dir
      type-featureA feature request or enhancement
      on Jan 5, 2019
    3. serhiy-storchaka commented on Jan 5, 2019

      @serhiy-storchaka
      Member

      This can be changed only after implementing PEP-572.

    4. tirkarthi commented on Jan 25, 2019

      @tirkarthi
      Member

      I believe a spot where assignment expressions are explicitly mentioned as not being included in the language, which is no longer the case

      Emily, I guess you were referring to this issue in https://bugs.python.org/issue35224#msg334331 . Now that PEP-572 implementation is merged this can be updated.

      Thanks

    5. tirkarthi commented on Jan 25, 2019

      @tirkarthi
      Member

      Ah sorry I think this could be a duplicate of bpo-34237 where you have added an update.

    6. tirkarthi commented on Nov 12, 2019

      @tirkarthi
      Member

      This is updated with 6357c95 . Closing this as fixed. Feel free to reopen if needed. Thanks.

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)end of lifedocsDocumentation in the Doc dirtype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions