Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Scope issue when calling components from list comprehension #11

Open
pauleveritt opened this issue Mar 29, 2020 · 1 comment
Open

Scope issue when calling components from list comprehension #11

pauleveritt opened this issue Mar 29, 2020 · 1 comment

Comments

@pauleveritt
Copy link
Contributor

I'm trying to do (in viewdom) an example of looping using a list comprehension. I was combining this with docs about Context and avoiding passing a value (in this case, prefix) down the component tree:

def Todo(label, prefix):
    return html('<li>{prefix}{label}</li>')


def TodoList(prefix, todos):
    return html('<ul>{[Todo(label, prefix) for label in todos]}</ul>')

This gives:

usage/examples/componentsH.py:12: in TodoList
    return html('<ul>{[Todo(label, prefix) for label in todos]}</ul>')
../../.venv/lib/python3.8/site-packages/tagged/__init__.py:84: in __tag
    values.append(eval(expr, f_globals, f_locals))
:1: in <module>
    ???
:1: in <listcomp>
    ???
E   NameError: name 'prefix' is not defined

Should a value in the scope of a function be available to a list comprehension?

@jviide
Copy link
Owner

jviide commented Apr 8, 2020

Ah, seems that this is an issue in tagged that's probably going to be tricky to fix.

Meanwhile, I suggest you work around the issue by moving the list comprehension out of the template:

def TodoList(prefix, todos):
    items = [Todo(label, prefix) for label in todos]
    return html('<ul>{items}</ul>')

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants