You are not logged in.

#1 2005-10-23 18:38:29

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

testing varible types in python

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

#2 2005-10-24 00:12:44

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: testing varible types in python

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

#3 2005-10-24 15:43:02

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: testing varible types in python

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

#4 2005-10-24 15:53:44

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: testing varible types in python

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

#5 2005-10-24 16:57:12

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: testing varible types in python

phrakture wrote:

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

#6 2005-10-25 02:51:57

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: testing varible types in python

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

#7 2005-10-25 03:22:12

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: testing varible types in python

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

#8 2005-10-25 19:43:09

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: testing varible types in python

Dusty wrote:

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

Board footer

Powered by FluxBB