Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > detecting variable types

Reply
Thread Tools

detecting variable types

 
 
Jay
Guest
Posts: n/a
 
      09-22-2004
I'm sure this is a really dumb question, but how do you detect a variable
type in Python?

For example, I want to know if the variable "a" is a list of strings or a
single string. How do I do this?


 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      09-22-2004
Jay wrote:
> I'm sure this is a really dumb question, but how do you detect a variable
> type in Python?
>
> For example, I want to know if the variable "a" is a list of strings or a
> single string. How do I do this?


Use the builtin function "type()", but note the following:

1. Variables don't actually have types in Python (and they're usually
called "names" in Python, for various reasons), but the data they are
currently bound to does have a type and that's what type() returns.
Often the distinction won't matter to you...

2. Most of the time people trying to do what you are probably trying
to do are going about things the wrong way, often from experience
with other languages. Lots of Python folks would be happy to introduce
you to "better" ways to do things, if you'll explain the use case
and tell us what you're actually trying to accomplish. (Of course,
using "type()" will work, but it's rarely considered the best
approach in the Python community.)

-Peter
 
Reply With Quote
 
 
 
 
Chris Green
Guest
Posts: n/a
 
      09-22-2004
"Jay" <(E-Mail Removed)> writes:

> I'm sure this is a really dumb question, but how do you detect a variable
> type in Python?
>
> For example, I want to know if the variable "a" is a list of strings or a
> single string. How do I do this?


>>> type("a")

<type 'str'>
>>> type(1)

<type 'int'>

you should look at the types module as well

>>> type("a") == types.StringType

True
--
Chris Green <(E-Mail Removed)>
This is my signature. There are many like it but this one is mine.
 
Reply With Quote
 
Jay
Guest
Posts: n/a
 
      09-22-2004
Thanks, Peter.

Here's what I'm trying to do:

I have a function like this:

def func(**params):

# if params[key1] is a single string
# do something with params[key1]

# if params[key1] is a list of strings
for val in params[key1]:
# do something

Could you suggest a better way to do this without detecting the type?


Jay.


"Peter Hansen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jay wrote:
> > I'm sure this is a really dumb question, but how do you detect a

variable
> > type in Python?
> >
> > For example, I want to know if the variable "a" is a list of strings or

a
> > single string. How do I do this?

>
> Use the builtin function "type()", but note the following:
>
> 1. Variables don't actually have types in Python (and they're usually
> called "names" in Python, for various reasons), but the data they are
> currently bound to does have a type and that's what type() returns.
> Often the distinction won't matter to you...
>
> 2. Most of the time people trying to do what you are probably trying
> to do are going about things the wrong way, often from experience
> with other languages. Lots of Python folks would be happy to introduce
> you to "better" ways to do things, if you'll explain the use case
> and tell us what you're actually trying to accomplish. (Of course,
> using "type()" will work, but it's rarely considered the best
> approach in the Python community.)
>
> -Peter



 
Reply With Quote
 
djw
Guest
Posts: n/a
 
      09-22-2004
Jay wrote:

> Thanks, Peter.
>
> Here's what I'm trying to do:
>
> I have a function like this:
>
> def func(**params):
>
> # if params[key1] is a single string
> # do something with params[key1]
>
> # if params[key1] is a list of strings
> for val in params[key1]:
> # do something
>
> Could you suggest a better way to do this without detecting the type?
>
>
> Jay.
>
>
> "Peter Hansen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Jay wrote:
>> > I'm sure this is a really dumb question, but how do you detect a

> variable
>> > type in Python?
>> >
>> > For example, I want to know if the variable "a" is a list of strings or

> a
>> > single string. How do I do this?

>>
>> Use the builtin function "type()", but note the following:
>>
>> 1. Variables don't actually have types in Python (and they're usually
>> called "names" in Python, for various reasons), but the data they are
>> currently bound to does have a type and that's what type() returns.
>> Often the distinction won't matter to you...
>>
>> 2. Most of the time people trying to do what you are probably trying
>> to do are going about things the wrong way, often from experience
>> with other languages. Lots of Python folks would be happy to introduce
>> you to "better" ways to do things, if you'll explain the use case
>> and tell us what you're actually trying to accomplish. (Of course,
>> using "type()" will work, but it's rarely considered the best
>> approach in the Python community.)
>>
>> -Peter



One obvious way, in this case is to always pass in a list of strings. A
single string would be passed in as a list with a single string item in it.

def func( stringlist ):
for s in stringlist:
#do something with s

func( [ "single string" ] )
func( [ "more", "than", "one", "string" ] )

Other than that, if the functions are really doing something very different
given different parameter types, then I would make separate functions.

-Don

 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      09-22-2004

"Jay" <(E-Mail Removed)> wrote in message
news:ciskpq$7f2$(E-Mail Removed)...
> Thanks, Peter.
>
> Here's what I'm trying to do:
>
> I have a function like this:
>
> def func(**params):
>
> # if params[key1] is a single string
> # do something with params[key1]
>
> # if params[key1] is a list of strings
> for val in params[key1]:
> # do something
>
> Could you suggest a better way to do this without detecting the type?


I'd strongly suggest making the value of "key1" a list in
all cases, including the case where there is no value:
that is, an empty list.

It makes your processing logic a lot simpler, and even if
you can't make the caller do it that way, the function/method
to preprocess is fairly simple (and also takes care of the
"key not found" case.)

John Roth
>
>
> Jay.
>
>
> "Peter Hansen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Jay wrote:
>> > I'm sure this is a really dumb question, but how do you detect a

> variable
>> > type in Python?
>> >
>> > For example, I want to know if the variable "a" is a list of strings or

> a
>> > single string. How do I do this?

>>
>> Use the builtin function "type()", but note the following:
>>
>> 1. Variables don't actually have types in Python (and they're usually
>> called "names" in Python, for various reasons), but the data they are
>> currently bound to does have a type and that's what type() returns.
>> Often the distinction won't matter to you...
>>
>> 2. Most of the time people trying to do what you are probably trying
>> to do are going about things the wrong way, often from experience
>> with other languages. Lots of Python folks would be happy to introduce
>> you to "better" ways to do things, if you'll explain the use case
>> and tell us what you're actually trying to accomplish. (Of course,
>> using "type()" will work, but it's rarely considered the best
>> approach in the Python community.)
>>
>> -Peter

>
>


 
Reply With Quote
 
Tuure Laurinolli
Guest
Posts: n/a
 
      09-22-2004
Jay wrote:

> def func(**params):
>
> # if params[key1] is a single string
> # do something with params[key1]
>
> # if params[key1] is a list of strings
> for val in params[key1]:
> # do something
>
> Could you suggest a better way to do this without detecting the type?


def func_for_string(astring):
pass

def func_for_a_list_of_strings(alist):
pass


Also, top posting is evil. See
http://www.cs.tut.fi/~jkorpela/usenet/brox.html
 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      09-22-2004
"Jay" <(E-Mail Removed)> wrote in message
news:ciskpq$7f2$(E-Mail Removed)...

> Here's what I'm trying to do:
>
> I have a function like this:
>
> def func(**params):
>
> # if params[key1] is a single string
> # do something with params[key1]
>
> # if params[key1] is a list of strings
> for val in params[key1]:
> # do something
>
> Could you suggest a better way to do this without detecting the type?


I don't see anything particularly wrong with detecting the type this way:

if isinstance(params[key1], list):
for val in params[key1]:
# do something
else:
# do something with params[key1]

Of course that won't work for other kinds of sequences, but if that's what
you want, then that's what you want.


 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      09-22-2004
On 2004-09-22, Andrew Koenig <(E-Mail Removed)> wrote:
> "Jay" <(E-Mail Removed)> wrote in message
> news:ciskpq$7f2$(E-Mail Removed)...
>
>> Here's what I'm trying to do:
>>
>> I have a function like this:
>>
>> def func(**params):
>>
>> # if params[key1] is a single string
>> # do something with params[key1]
>>
>> # if params[key1] is a list of strings
>> for val in params[key1]:
>> # do something
>>
>> Could you suggest a better way to do this without detecting the type?

>
> I don't see anything particularly wrong with detecting the type this way:
>
> if isinstance(params[key1], list):
> for val in params[key1]:
> # do something
> else:
> # do something with params[key1]
>
> Of course that won't work for other kinds of sequences, but if that's what
> you want, then that's what you want.


When I write functions that accept either a list or a single
object, I usually "normalize" the paramter into a list and then
the rest of the function just operates on lists:

if not isinstance(myParameter,list):
myParameter = [myParameter]

[...]

for p in myParameter:
<do whatever>

[...]

--
Grant Edwards grante Yow! Is this where people
at are HOT and NICE and they
visi.com give you TOAST for FREE??
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      09-22-2004
On Wed, 22 Sep 2004 15:45:55 -0400, "Jay" <(E-Mail Removed)> wrote:

>Thanks, Peter.

Please don't top-post, ok? Notice what it does to the order
of your comments and mine vs Peters'. Insert your comments below
quoted material it refers to, then order can be preserved through
several generations. Stacking wholes monolithically preserves order
too, but it gets harder to indicate where in the following monoliths
your top-posted comments apply.

>
>Here's what I'm trying to do:
>
>I have a function like this:
>
>def func(**params):
>
> # if params[key1] is a single string
> # do something with params[key1]
>
> # if params[key1] is a list of strings
> for val in params[key1]:
> # do something
>
>Could you suggest a better way to do this without detecting the type?
>

That particular distinction is extra nasty because strings are also iterable.

One trouble with type(arg)==list or type(arg)==str is that if your calling
program wants to pass a list or str subtype later, you will have to change
your func code to detect the new type, even though it behaves the same.

So you usually better off with an isinstance(arg, str) than a type(arg)==str test.
Notice,

>>> class S(str): pass

...
>>> s=S('hello')
>>>
>>> s

'hello'
>>> type('hello')

<type 'str'>
>>> type(s)

<class '__main__.S'>
>>> isinstance('hello', str)

True
>>> isinstance(s, str)

True

IOW,
>>> type('hello')==str

True
>>> type(s)==str

False

Sometimes you do actually need that distinction though.

>
>Jay.
>
>
>"Peter Hansen" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> Jay wrote:
>> > I'm sure this is a really dumb question, but how do you detect a

>variable
>> > type in Python?
>> >
>> > For example, I want to know if the variable "a" is a list of strings or

>a
>> > single string. How do I do this?

>>
>> Use the builtin function "type()", but note the following:
>>
>> 1. Variables don't actually have types in Python (and they're usually
>> called "names" in Python, for various reasons), but the data they are
>> currently bound to does have a type and that's what type() returns.
>> Often the distinction won't matter to you...
>>
>> 2. Most of the time people trying to do what you are probably trying
>> to do are going about things the wrong way, often from experience
>> with other languages. Lots of Python folks would be happy to introduce
>> you to "better" ways to do things, if you'll explain the use case
>> and tell us what you're actually trying to accomplish. (Of course,
>> using "type()" will work, but it's rarely considered the best
>> approach in the Python community.)
>>
>> -Peter

>
>


Regards,
Bengt Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting support for event types RobG Javascript 15 08-22-2008 03:55 AM
Detecting mime types smcardle@smcardle.com Java 1 09-23-2007 07:57 AM
Detecting the opening of file types tazmaster2@gmail.com Java 1 05-07-2007 12:25 AM
detecting basic types Lasse Skyum C++ 6 12-24-2003 09:43 AM
Detecting document types Chris Java 0 11-24-2003 03:05 AM



Advertisments