[Zope] Re: Testing types in TAL

Tres Seaver tseaver at zope.com
Fri Nov 12 14:12:35 EST 2004


Alec Mitchell wrote:
> So I've run into a minor TAL problem recently.  I'm trying to test to see if 
> an object is a List or a Tuple (or more generally a sequence that isn't a 
> string), but can't figure out a way.  I've tried the following:
> 
>    python:isinstance(obj, list)
> doesn't work (it doesn't recoginze 'list' as a 'class, type or tuple of 
> classes and types'), strangely:
>    python:isinstance(obj, str)
> works ('str' is ok, but not 'list' or 'tuple'?).  Also:
>    python:list(obj)
> works despite the fact that 'list' is not recognized as a class!
> 
> using:
>    python:type(obj) is type([])
> fails because type() is not recognized in TAL.
> 
> using:
>    python:hasattr(obj, '__iter__')
> fails also, though I'm not sure why (probably TAL doesn't bother with 
> variables starting with '_').
> 
> And it's impossible to pull 'ListType' etc. in using modules['types'], because 
> of the lack of security assertions on the 'types' module.
> 
> There must be a way to do a simple type check for a tuple without a python 
> script, no?  Right now I'm using:
> 
> tal:define="dictish nocall:obj/keys|nothing;
>             listish python:not dictish and len(obj) and (len(obj[0]) != 1)"
> 
> Which is far too kludgy, brittle, and plain incorrect for my liking.

'same_type' is what you want:

   http://www.plope.com/Books/2_7Edition/BasicScripting.stx

Tres.
-- 
===============================================================
Tres Seaver                                tseaver at zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com



More information about the Zope mailing list