I have a class which hold some variables as strings. e.g.
x = "1" y = "2.0" z = "timedelta(seconds=10)" As you can see above the variables are actually int/float/timedelta-types. I need to return these variables with their real type. (x should be returned as a int, y should be returned as a float, z should be returned as datetime.timedelta)
I was thinking of changing the getattr and try to find out the type before returning the value, but the small test I did, does not seem to work:
from datetime import datetime, timedelta def test(): string = "timedelta(seconds=10)" x = eval(string) print x print type(x) if type(x) == 'datetime.timedelta': print "YES" else: print "NO" The output is:
0:00:10 <type 'datetime.timedelta'> NO why is the if-case returning false? is there a better way to return these variables with the real type?