'dict' is a lambda in templates?

7 views
Skip to first unread message

Eli Stevens (Gmail)

unread,
Jun 26, 2014, 1:39:09 PM6/26/14
to gen...@googlegroups.com
I just tried to use isinstance(myvar, dict) in a template, but got the
following exception:

TypeError: isinstance() arg 2 must be a class, type, or tuple of
classes and types

I got the repr(dict) and it said it was a lambda. Putting in the following:

<py:with vars="content_str = inspect.getsource(dict);">
${content_str}

I get the output from content_str:

'dict': lambda **kw: kw,

I feel fairly confident that this isn't due to anything I've written
directly, but haven't been able to rule out flask or the rest of the
stack I'm using.

Is this a known genshi-ism? Any idea why this would be useful?

For the record (and future searchers), isinstance(myvar, type({})
works well enough, though

Thanks,
Eli

Simon Cross

unread,
Jun 30, 2014, 4:32:30 AM6/30/14
to gen...@googlegroups.com
For me:

from genshi.template.markup import MarkupTemplate

t = MarkupTemplate("""<div>${isinstance(x, dict)}</div>""")

print t.generate(x=1).render()
print t.generate(x={}).render()

prints:

<div>False</div>
<div>True</div>

So it doesn't look like Genshi doesn't do anything odd.

Schiavo
Simon
Reply all
Reply to author
Forward
0 new messages