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

eval() function in List Comprehension doesn't work #49492

Closed
JiafeiPeng mannequin opened this issue Feb 13, 2009 · 9 comments
Closed

eval() function in List Comprehension doesn't work #49492

JiafeiPeng mannequin opened this issue Feb 13, 2009 · 9 comments

Comments

@JiafeiPeng
Copy link
Mannequin

JiafeiPeng mannequin commented Feb 13, 2009

BPO 5242
Nosy @birkenfeld, @ezio-melotti
Files
  • unnamed
  • unnamed
  • 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-14.12:36:08.326>
    created_at = <Date 2009-02-13.09:42:04.755>
    labels = []
    title = "eval() function in List Comprehension doesn't work"
    updated_at = <Date 2009-02-14.12:36:08.314>
    user = 'https://bugs.python.org/JiafeiPeng'

    bugs.python.org fields:

    activity = <Date 2009-02-14.12:36:08.314>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-02-14.12:36:08.326>
    closer = 'georg.brandl'
    components = []
    creation = <Date 2009-02-13.09:42:04.755>
    creator = 'JiafeiPeng'
    dependencies = []
    files = ['13065', '13066']
    hgrepos = []
    issue_num = 5242
    keywords = []
    message_count = 9.0
    messages = ['81890', '81891', '81895', '81896', '81897', '81898', '81918', '81926', '82033']
    nosy_count = 5.0
    nosy_names = ['georg.brandl', 'QuantumTim', 'ezio.melotti', 'hagen', 'JiafeiPeng']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue5242'
    versions = ['Python 3.0']

    @JiafeiPeng
    Copy link
    Mannequin Author

    JiafeiPeng mannequin commented Feb 13, 2009

    eval() function in List Comprehension doesn't work.
    please see the under codes:

    canBusType = 'CANdiag'
    result = [eval('canBusType') for i in range(3)]
    NameError: name 'canBusType' is not defined

    It did work in Python2.5 or 2.6. The expected result is
    ['CANdiag', 'CANdiag', 'CANdiag'].

    @hagen
    Copy link
    Mannequin

    hagen mannequin commented Feb 13, 2009

    I can't reproduce this. For me it works as expected.

    @ezio-melotti
    Copy link
    Member

    I can't reproduce it either, tested with Py3 (on Linux and Windows) and
    with Py2.[456], it worked fine everywhere.

    Does your eval() work properly outside listcomps?

    @JiafeiPeng
    Copy link
    Mannequin Author

    JiafeiPeng mannequin commented Feb 13, 2009

    Hallo Mr. Fürstenau,

    thank you for the quick response.
    Have you tried it with Python 3.0

    The Error report is truely:
    NameError: name 'canBusType' is not defined

    Someone else reported the same problem in internet too.

    Best regards, mit freundlichen Grüßen,

    Jiafei Peng

    Softwareentwickler / Embedded System Software (EF-F2)
    Software developer / Embedded System Software

    IAV GmbH
    Nordhoffstr. 5
    38518 Gifhorn
    GERMANY

    Phone: +49 5371 805-2817
    Fax:+49 5371 805-1330

    E-mail: <mailto:Jiafei.Peng@iav.de>
    Internet: http://www.iav.de

    IAV GmbH
    Sitz/Registered Office: Berlin
    Registergericht/Registration Court: Amtsgericht Charlottenburg
    Registernummer/Company Registration Number: HRB 21 280
    Geschäftsführer/Managing Directors: Kurt Blumenröder, Michael Schubert

    Hagen Fürstenau <report@bugs.python.org>
    13.02.2009 10:54
    Bitte antworten an
    Python tracker <report@bugs.python.org>

    An
    jiafei.peng@iav.de
    Kopie

    Thema
    [bpo-5242] eval() function in List Comprehension doesn't work

    Hagen Fürstenau <hfuerstenau@gmx.net> added the comment:

    I can't reproduce this. For me it works as expected.

    ----------
    nosy: +hagen


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


    @JiafeiPeng
    Copy link
    Mannequin Author

    JiafeiPeng mannequin commented Feb 13, 2009

    Yes
    it does work properly outside listcomps.

                canBusType = 'CANdiag'
                result1 = eval('canBusType')
                result2 = [eval('canBusType'), eval('canBusType'), eval(
    'canBusType')]
                result3 = [eval('canBusType') for i in range(3)]
    
    result1 = 'CANdiag'
    result2 =['CANdiag' 'CANdiag' 'CANdiag']
    for result3:
    NameError: name 'canBusType' is not defined

    Best regards, mit freundlichen Grüßen,

    Jiafei Peng

    Softwareentwickler / Embedded System Software (EF-F2)
    Software developer / Embedded System Software

    IAV GmbH
    Nordhoffstr. 5
    38518 Gifhorn
    GERMANY

    Phone: +49 5371 805-2817
    Fax:+49 5371 805-1330

    E-mail: <mailto:Jiafei.Peng@iav.de>
    Internet: http://www.iav.de

    IAV GmbH
    Sitz/Registered Office: Berlin
    Registergericht/Registration Court: Amtsgericht Charlottenburg
    Registernummer/Company Registration Number: HRB 21 280
    Geschäftsführer/Managing Directors: Kurt Blumenröder, Michael Schubert

    Ezio Melotti <report@bugs.python.org>
    13.02.2009 11:04
    Bitte antworten an
    Python tracker <report@bugs.python.org>

    An
    jiafei.peng@iav.de
    Kopie

    Thema
    [bpo-5242] eval() function in List Comprehension doesn't work

    Ezio Melotti <ezio.melotti@gmail.com> added the comment:

    I can't reproduce it either, tested with Py3 (on Linux and Windows) and
    with Py2.[456], it worked fine everywhere.

    Does your eval() work properly outside listcomps?

    ----------
    nosy: +ezio.melotti


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


    @birkenfeld
    Copy link
    Member

    Ezio: this happens inside a function, like this:

    def f():
        canBusType = 'CANdiag'
        result = [eval('canBusType') for i in range(3)]

    This is expected, and won't easily fix. The reason is that list
    comprehensions in 3.x use a function namespace "under the hood" (in 2.x,
    they were implemented like a simple for loop). Because inner functions
    need to know what names to get from what enclosing namespace, the names
    referenced in eval() can't come from enclosing functions. They must
    either be locals or globals.

    Of course, the question to the OP is why eval() is needed anyway.

    @QuantumTim
    Copy link
    Mannequin

    QuantumTim mannequin commented Feb 13, 2009

    If you know what variable you are going to be eval-ing, or at least,
    have a list of those that might be eval-ed, you can get around this
    issue by making sure they are explicitly referenced in the inner scope
    (i.e., in the list comprehension). For example, even though list
    comprehensions work in 2.x, generator expressions don't, but this hack
    does (on 2.4 at least):

    def f():
      canBusType = 'CANdiag'
      return (eval('canBusType') for i in range(3) if True or canBusType)

    By putting a semantically vacuous reference to canBusType (and any
    other variables you want) you make sure they are usable from within the
    eval as well.

    @ezio-melotti
    Copy link
    Member

    eval() is probably already an hack, there's no need to add another hack
    to make it work. It's better to just get rid of eval() and find a better
    way to do what you want to do.

    @birkenfeld
    Copy link
    Member

    I agree.

    @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
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants