Skip to content

vars() assignment fails silently when assignments made later #49447

Closed

Description

@pest
mannequin
BPO 5197

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 2009-02-10.07:35:43.740>
created_at = <Date 2009-02-10.05:14:41.731>
labels = ['type-bug', 'invalid']
title = 'vars() assignment fails silently when assignments made later'
updated_at = <Date 2009-02-10.07:35:43.727>
user = 'https://bugs.python.org/pest'

bugs.python.org fields:

activity = <Date 2009-02-10.07:35:43.727>
actor = 'ocean-city'
assignee = 'none'
closed = True
closed_date = <Date 2009-02-10.07:35:43.740>
closer = 'ocean-city'
components = ['None']
creation = <Date 2009-02-10.05:14:41.731>
creator = 'pest'
dependencies = []
files = []
hgrepos = []
issue_num = 5197
keywords = []
message_count = 2.0
messages = ['81530', '81533']
nosy_count = 2.0
nosy_names = ['ocean-city', 'pest']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue5197'
versions = ['Python 2.5']

Activity

  1. pest commented on Feb 10, 2009

    pestmannequin
    MannequinAuthor

    Assignments to vars() behave incorrectly if those variables are use
    later on in a function.

    For example, try this:

    ======
    #!/usr/bin/python

    def zz():
            for i in [1,2,3]:
                    for j in [4,5,6]:
                            vars()['fw_%s_%s' % (j,i)] = 'test %s %s' % (j, i)
                            print vars()['fw_%s_%s' % (j,i)]
    
            fw_4_2 = 'zz'
            print fw_4_2
    
    zz()

    ======

    The assignments work fine up till 4_2. At 4_2, the assignment apparently
    fails silently, and the print raises a keyError exception.

  2. added
    type-bug
    on Feb 10, 2009
  3. ocean-city commented on Feb 10, 2009

    ocean-citymannequin
    Mannequin

    According to http://docs.python.org/library/functions.html#vars

    The returned dictionary should not be modified: the effects on the
    corresponding symbol table are undefined.

    Same can be said for locals() as well.

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

    type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions