Skip to content

add shorthand global and nonlocal statements #48449

Closed

Description

@benjaminp
BPO 4199
Nosy @akuchling, @birkenfeld, @rhettinger, @gpshead, @amauryfa, @benjaminp, @bitdancer, @ericsnowcurrently
Files
  • global_nonlocal_shorthand.patch
  • global_nonlocal_shorthand2.patch
  • 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 2013-06-30.14:12:36.980>
    created_at = <Date 2008-10-24.22:32:14.485>
    labels = ['interpreter-core', 'type-feature']
    title = 'add shorthand global and nonlocal statements'
    updated_at = <Date 2013-06-30.14:12:36.945>
    user = 'https://github.com/benjaminp'

    bugs.python.org fields:

    activity = <Date 2013-06-30.14:12:36.945>
    actor = 'gregory.p.smith'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-06-30.14:12:36.980>
    closer = 'gregory.p.smith'
    components = ['Interpreter Core']
    creation = <Date 2008-10-24.22:32:14.485>
    creator = 'benjamin.peterson'
    dependencies = []
    files = ['11884', '12254']
    hgrepos = []
    issue_num = 4199
    keywords = ['patch', 'needs review']
    message_count = 20.0
    messages = ['75193', '77089', '77134', '77159', '88874', '99705', '99958', '99965', '99966', '99977', '99978', '100015', '100043', '100188', '110694', '110698', '155723', '155865', '192049', '192070']
    nosy_count = 10.0
    nosy_names = ['jhylton', 'akuchling', 'georg.brandl', 'rhettinger', 'gregory.p.smith', 'amaury.forgeotdarc', 'benjamin.peterson', 'r.david.murray', 'BreamoreBoy', 'eric.snow']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue4199'
    versions = ['Python 3.4']

    Activity

    1. benjaminp commented on Oct 24, 2008

      @benjaminp
      ContributorAuthor

      PEP-3104 says that the nonlocal and global statements should allow a
      shorthand. ("global x; x = 3" == "global x = 3") This patch implements that.

    2. benjaminp commented on Dec 6, 2008

      @benjaminp
      ContributorAuthor

      Please review.

    3. amauryfa commented on Dec 6, 2008

      @amauryfa
      Contributor
      • the add_ast_fields() function seems to be added by this patch, but it
        is already in svn.

      • Are 1-tuple supported?

          t = [1]
          global a, = t
    4. benjaminp commented on Dec 6, 2008

      @benjaminp
      ContributorAuthor

      I think I may have been merging add_ast_fields when I wrote the patch.

      Here's a new patch that handles "global x, = (5,)". To do it, I added a
      new flag* to the Global and Nonlocal AST objects that indicates whether
      the value needs to be unpacked or not.

      • The flag is an int. The bool AST type was removed in py3k.
    5. birkenfeld commented on Jun 4, 2009

      @birkenfeld
      Member

      Postponed to 3.2.

    6. akuchling commented on Feb 22, 2010

      @akuchling
      Contributor

      Bumping priority so this doesn't get forgotten before 3.2; it seems important because it fixes noncompliance with a PEP.

    7. birkenfeld commented on Feb 23, 2010

      @birkenfeld
      Member

      "High" is not high enough :)

    8. jhylton commented on Feb 23, 2010

      jhyltonmannequin
      Mannequin

      Is deferred blocker a higher priority?

      Jeremy

      On Tue, Feb 23, 2010 at 4:37 PM, Georg Brandl <report@bugs.python.org> wrote:

      Georg Brandl <georg@python.org> added the comment:

      "High" is not high enough :)

      ----------
      priority: high -> deferred blocker


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue4199\>


    9. birkenfeld commented on Feb 23, 2010

      @birkenfeld
      Member

      I interpret the "Priority" list view contents to be sorted by priority, so "deferred blocker" is the second-highest.

    10. jhylton commented on Feb 23, 2010

      jhyltonmannequin
      Mannequin

      On Sat, Dec 6, 2008 at 1:28 PM, Benjamin Peterson
      <report@bugs.python.org> wrote:

      Benjamin Peterson <musiccomposition@gmail.com> added the comment:

      I think I may have been merging add_ast_fields when I wrote the patch.

      Here's a new patch that handles "global x, = (5,)". To do it, I added a
      new flag* to the Global and Nonlocal AST objects that indicates whether
      the value needs to be unpacked or not.

      You shouldn't need to do this. The unpack is implied if the number of
      identifiers is greater than 1.

      Jeremy

      • The flag is an int. The bool AST type was removed in py3k.

      Added file: http://bugs.python.org/file12254/global_nonlocal_shorthand2.patch


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue4199\>



      Python-bugs-list mailing list
      Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/jeremy%40alum.mit.edu

    11. jhylton commented on Feb 23, 2010

      jhyltonmannequin
      Mannequin

      I also notice that the Grammar in the PEP is more complicated:
      nonlocal_stmt ::=
      "nonlocal" identifier ("," identifier)*
      ["=" (target_list "=")+ expression_list]
      | "nonlocal" identifier augop expression_list

      The Grammar in the patch is:
      +global_stmt: 'global' NAME (',' NAME)* [','] ['=' testlist]
      +nonlocal_stmt: 'nonlocal' NAME (',' NAME)* [','] ['=' testlist]

      It appears that the PEP is trying to support:

      nonlocal x = y = z = 1
      nonlocal a, b = c, d = 1

      If we're going to support the PEP as written, I think we need to
      modify Global() and Nonlocal() to look exactly like Assign(), but add
      an extra check to verify that all of the expressions in the targets
      are Name, List, or Tuple. You'd probably want to check this at the
      time you are generating the AST, so that you're not stuck with some
      extra state in the compiler traversal about whether you are generating
      code for a Global() or an Assign().

      This approach makes the compiler code very simple. We use exactly the
      same code for Global(), Nonlocal(), and Assign(). It does have the
      downside that you need to enforce this unwritten constraint of the AST
      in ast.c and in the ast module.

      It also means that the AST will change in a non-backwards compatible
      way. I don't see how to do that given that we're also changing the
      language spec. (Do you want to include the spec change in your
      patch?)

      Jeremy

      On Tue, Feb 23, 2010 at 6:41 PM, Jeremy Hylton <jeremy@alum.mit.edu> wrote:

      On Sat, Dec 6, 2008 at 1:28 PM, Benjamin Peterson
      <report@bugs.python.org> wrote:
      >
      > Benjamin Peterson <musiccomposition@gmail.com> added the comment:
      >
      > I think I may have been merging add_ast_fields when I wrote the patch.
      >
      > Here's a new patch that handles "global x, = (5,)". To do it, I added a
      > new flag* to the Global and Nonlocal AST objects that indicates whether
      > the value needs to be unpacked or not.

      You shouldn't need to do this.  The unpack is implied if the number of
      identifiers is greater than 1.

      Jeremy

      > * The flag is an int. The bool AST type was removed in py3k.
      >
      > Added file: http://bugs.python.org/file12254/global_nonlocal_shorthand2.patch
      >
      > _______________________________________
      > Python tracker <report@bugs.python.org>
      > <http://bugs.python.org/issue4199\>
      > _______________________________________
      > _______________________________________________
      > Python-bugs-list mailing list
      > Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/jeremy%40alum.mit.edu
      >
      >

    12. birkenfeld commented on Feb 24, 2010

      @birkenfeld
      Member

      I also notice that the Grammar in the PEP is more complicated:
      nonlocal_stmt ::=
      "nonlocal" identifier ("," identifier)*
      ["=" (target_list "=")+ expression_list]
      | "nonlocal" identifier augop expression_list

      The Grammar in the patch is:
      +global_stmt: 'global' NAME (',' NAME)* [','] ['=' testlist]
      +nonlocal_stmt: 'nonlocal' NAME (',' NAME)* [','] ['=' testlist]

      It appears that the PEP is trying to support:

      nonlocal x = y = z = 1
      nonlocal a, b = c, d = 1

      It also tries to support augmented assignment; however I'm not sure what the semantics of that should be.

      Further, there is an ambiguity if too much freedom is allowed: what about

      global x = 1, y

      Is it declaring a global "x" and assigning a tuple, or declaring a global "x" and a global "y"?

      If we're going to support the PEP as written, I think we need to
      modify Global() and Nonlocal() to look exactly like Assign(), but add
      an extra check to verify that all of the expressions in the targets
      are Name, List, or Tuple. You'd probably want to check this at the
      time you are generating the AST, so that you're not stuck with some
      extra state in the compiler traversal about whether you are generating
      code for a Global() or an Assign().

      I would not support List or Tuple as targets. Same basic problem as
      above, and I don't see a use case.

      I see two possibilities for the actual syntax:

      1. global *either* supports multiple identifiers, *or* one identifier
        and an assignment.

      2. global always supports multiple identifiers, each with an optional
        assignment; tuples need parentheses.

      In both cases, I would keep it simple and not allow multiple targets or
      augmented assignment.

    13. jhylton commented on Feb 24, 2010

      jhyltonmannequin
      Mannequin

      I guess there's some question about whether the syntax in the PEP was
      considered carefully when it was approved. If so, I'm not sure that
      we want to re-open the discussion. On the other hand, it's been a
      long time since the PEP was approved and we do have a moratorium on
      language changes, so it doesn't hurt to slow things down.

      I'd argue that the intent of the PEP is pretty clear. You can take
      any assignment statement where the LHS doesn't have subscripts, put a
      nonlocal or global in front of it, and it means that all those
      assignments are to global/nonlocal variables.

      Jeremy

      On Wed, Feb 24, 2010 at 4:20 AM, Georg Brandl <report@bugs.python.org> wrote:

      Georg Brandl <georg@python.org> added the comment:

      > I also notice that the Grammar in the PEP is more complicated:
      > nonlocal_stmt ::=
      >     "nonlocal" identifier ("," identifier)*
      >                ["=" (target_list "=")+ expression_list]
      >   | "nonlocal" identifier augop expression_list
      >
      > The Grammar in the patch is:
      > +global_stmt: 'global' NAME (',' NAME)* [','] ['=' testlist]
      > +nonlocal_stmt: 'nonlocal' NAME (',' NAME)* [','] ['=' testlist]
      >
      > It appears that the PEP is trying to support:
      >
      > nonlocal x = y = z = 1
      > nonlocal a, b = c, d = 1

      It also tries to support augmented assignment; however I'm not sure what the semantics of that should be.

      Further, there is an ambiguity if too much freedom is allowed: what about

        global x = 1, y

      Is it declaring a global "x" and assigning a tuple, or declaring a global "x" and a global "y"?

      > If we're going to support the PEP as written, I think we need to
      > modify Global() and Nonlocal() to look exactly like Assign(), but add
      > an extra check to verify that all of the expressions in the targets
      > are Name, List, or Tuple.  You'd probably want to check this at the
      > time you are generating the AST, so that you're not stuck with some
      > extra state in the compiler traversal about whether you are generating
      > code for a Global() or an Assign().

      I would not support List or Tuple as targets.  Same basic problem as
      above, and I don't see a use case.

      I see two possibilities for the actual syntax:

      1. global *either* supports multiple identifiers, *or* one identifier
        and an assignment.

      2. global always supports multiple identifiers, each with an optional
        assignment; tuples need parentheses.

      In both cases, I would keep it simple and not allow multiple targets or
      augmented assignment.

      ----------


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue4199\>


    14. birkenfeld commented on Feb 27, 2010

      @birkenfeld
      Member

      I do think a brief discussion after the moratorium is over would be good.

    15. BreamoreBoy commented on Jul 18, 2010

      BreamoreBoymannequin
      Mannequin

      I'm not sure as to the status of this. Could it go back to (say) 3.3, is it still a candidate for 3.2(.x), or what?

    16. benjaminp commented on Jul 18, 2010

      @benjaminp
      ContributorAuthor

      2010/7/18 Mark Lawrence <report@bugs.python.org>:

      Mark Lawrence <breamoreboy@yahoo.co.uk> added the comment:

      I'm not sure as to the status of this.  Could it go back to (say) 3.3, is it still a candidate for 3.2(.x), or what?

      It can't do anything until 3.3.

    17. bitdancer commented on Mar 14, 2012

      @bitdancer
      Member

      We could also just decide we don't need it :)

      If we do (I haven't read the PEP) does a statement with an assignment make the variable global in that scope, or does it only affect the global variable for the duration of the assignment, and otherwise the variable remains local in the scope? (I don't know which to guess, and the ambiguity is disturbing. I like the current clarity even if it is more typing.)

    18. rhettinger commented on Mar 15, 2012

      @rhettinger
      Contributor

      +1 on the feature as described in the PEP.

    19. akuchling commented on Jun 29, 2013

      @akuchling
      Contributor

      Bumping version to 3.4. I'll send a note to python-dev about this issue.

    20. gpshead commented on Jun 30, 2013

      @gpshead
      Member

      Closing and rejecting based on said discussion. http://mail.python.org/pipermail/python-dev/2013-June/127143.html

    21. 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

      interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions