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

add shorthand global and nonlocal statements #48449

Closed
benjaminp opened this issue Oct 24, 2008 · 20 comments
Closed

add shorthand global and nonlocal statements #48449

benjaminp opened this issue Oct 24, 2008 · 20 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@benjaminp
Copy link
Contributor

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']

    @benjaminp
    Copy link
    Contributor Author

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

    @benjaminp benjaminp added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Oct 24, 2008
    @benjaminp
    Copy link
    Contributor Author

    Please review.

    @benjaminp benjaminp added the type-feature A feature request or enhancement label Dec 6, 2008
    @amauryfa
    Copy link
    Member

    amauryfa commented Dec 6, 2008

    • 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

    @benjaminp
    Copy link
    Contributor Author

    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.

    @birkenfeld
    Copy link
    Member

    Postponed to 3.2.

    @akuchling
    Copy link
    Member

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

    @birkenfeld
    Copy link
    Member

    "High" is not high enough :)

    @jhylton
    Copy link
    Mannequin

    jhylton mannequin commented Feb 23, 2010

    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\>


    @birkenfeld
    Copy link
    Member

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

    @jhylton
    Copy link
    Mannequin

    jhylton mannequin commented Feb 23, 2010

    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

    @jhylton
    Copy link
    Mannequin

    jhylton mannequin commented Feb 23, 2010

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

    @birkenfeld
    Copy link
    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.

    @jhylton
    Copy link
    Mannequin

    jhylton mannequin commented Feb 24, 2010

    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\>


    @birkenfeld
    Copy link
    Member

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

    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 18, 2010

    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?

    @benjaminp
    Copy link
    Contributor Author

    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.

    @bitdancer
    Copy link
    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.)

    @rhettinger
    Copy link
    Contributor

    +1 on the feature as described in the PEP.

    @akuchling
    Copy link
    Member

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

    @gpshead
    Copy link
    Member

    gpshead commented Jun 30, 2013

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

    @gpshead gpshead closed this as completed Jun 30, 2013
    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    7 participants