Skip to content

Update ast_interactivity for more complex assignments. #10633

Open

Description

@Carreau

#10598 added the possibility to change the ast-interactivity mode for the last assignment of cells.
Though it work only for simple variable assignments.

Should we try to do that for more complex ones like a[0].b = something_else() , probably by copying the LHS of the assignment ?

Activity

  1. added this to the wishlist milestone on Jun 1, 2017
  2. takluyver commented on Jun 2, 2017

    @takluyver
    Member

    It's possible, and not that unusual, for something like a.foo to be a property which calls code on access. That's not necessarily a problem for an opt-in feature, but we should keep it in mind.

  3. jamadagni commented on Dec 12, 2024

    @jamadagni

    Hello. Would really be useful if this could be done. Basically we only want the value of the evaluated RHS before assignment, irrespective of the target of the assignment.

    As @takluyver says tt may be transformed to something else by property setters, that's understood and for getting that we should use the relevant getter only. But whatever is passed to the setter we would like to see that's all.

    Thanks!

  4. takluyver commented on Jan 6, 2025

    @takluyver
    Member

    The way the simple version works is that a cell ending in x = something() implicitly gets a bare x afterwards. Showing the value produced before the assignment gets a bit more complicated. You could read back the target and live with the possibility that it does something different, or triggers side effects:

    a.b = something()
    a.b

    You can't just evaluate the RHS again, because it can produce something different:

    a.b = next(iterator)
    next(iterator)  # Oops

    In theory, the best approach is to modify the code to something like this:

    _tmp = something()
    a.b = _tmp
    _tmp

    But that would be more complex than just sticking something on the end - you probably want to make sure the temporary variable name isn't already used, and delete the variable afterwards to avoid an unwanted reference.

  5. Carreau commented on Jan 6, 2025

    @Carreau
    MemberAuthor

    as we rewrite ast, we can use variable names that are invalid python identifiers.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions