You are not logged in.
Pages: 1
whats the best way to find out what type a varible is?
i think the following functions should work, but i figured there might be a better way to do it.
def isString(string):
try: string+' '
except: return 0
else: return 1
def isNumber(number):
try: number+0
except: return 0
else: return 1
Offline
Perhaps you could use the type() builtin?:
>>> def isString(string):
... return type(string) == type("a")
...
>>> isString("abc")
True
>>> isString(1)
False
>>>
However, I question why you want to find out the type of a variabl. The whole idea of python is to ignore types as much as possible. If you need a string and you have an int, call str(intval), int(strval) does the opposite.... list(), tuple(), float() are similar functions.
Dusty
Offline
in python2.4 you don't need the type thingy, every function to cerate a type (as str, int, float) is a type itself. so it's sufficent to use isinstance("as", str). also note that basestring is the supertype of normal and unciode strings.
Offline
I agree with Dusty here, if you want to worry about types then don't use a dynamic language... worrying about types in a dynamicly typed language is like worrying about the land speed of a speed boat.
Offline
I agree with Dusty here, if you want to worry about types then don't use a dynamic language... worrying about types in a dynamicly typed language is like worrying about the land speed of a speed boat.
you and cactus.... I love the analogies you guys come up with, I really do....
Offline
thanks for the help guys, this community always impresses me...
the reason i need this functioning is to tell the difference between an array and a string while calling len() on it.
len(array) gives array elements, len(string) gives number of characters, so if the varible is indeed a string, it would screw up everything i do to it after
Offline
array? I assume you mean list?
The question is why you are passing both a string and a lsit to the function that is going to check the length?
If you only expect to pass a list to the function, don't bother checking if its a string, and vice versa. If you do happen to accidentally pass a string to the method, you'll get an exception that will probably be easy to debug.
If you insist on doing type checking, you may as well be using a statically typed language and let the compiler do the work for you. :-)
If it is that you want to process both a string and an array, but you want to do it differently, then you may be on the right track, but I would probably put it each in separate functions or methods or something.
Dusty
Offline
If it is that you want to process both a string and an array, but you want to do it differently, then you may be on the right track, but I would probably put it each in separate functions or methods or something.
Yeah, agreed yet again. I would make a str_dosomething() and lst_dosomething() - in the calling code you're going to know if you have a list or string (you should), so you can call the proper function.
Offline
Pages: 1