[Zope] Test variable data type

Paul Winkler pw_lists@slinkp.com
Thu, 13 Mar 2003 17:30:05 -0500


On Thu, Mar 13, 2003 at 04:29:25PM -0500, Flavio wrote:
> Hi,
> 
> I wonder if someone knows how to test a variables data type. e.g. if
> <dtml-var a> is integer or string.

What are you really trying to do?
Doing different things based on type is not good:
it leads to code that can't treat objects polymorphically.
Somebody creates a string-like class and then discovers it
won't work with your code because you can't handle anything
that isn't really "a string".

In general, python makes it "easier to ask forgiveness
than permission" ... so you  might do things like:
    
# i want to use x as an integer
try:
    x = int(x)
except TypeError:
    return "x can't be used as an integer!"

# i want to use y as a string

try:
    y + ''
except TypeError:
    return "y is not string-like!"



-- 

Paul Winkler
http://www.slinkp.com
Look! Up in the sky! It's THE SOLID GOAT!
(random hero from isometric.spaceninja.com)