Connecting Tech Pros Worldwide Help | Site Map

detecting variable types

Jay
Guest
 
Posts: n/a
#1: Jul 18 '05
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?


Peter Hansen
Guest
 
Posts: n/a
#2: Jul 18 '05

re: detecting variable types


Jay wrote:[color=blue]
> 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?[/color]

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
Chris Green
Guest
 
Posts: n/a
#3: Jul 18 '05

re: detecting variable types


"Jay" <wjjeonk@hotmail.com> writes:
[color=blue]
> 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?[/color]
[color=blue][color=green][color=darkred]
>>> type("a")[/color][/color][/color]
<type 'str'>[color=blue][color=green][color=darkred]
>>> type(1)[/color][/color][/color]
<type 'int'>

you should look at the types module as well
[color=blue][color=green][color=darkred]
>>> type("a") == types.StringType[/color][/color][/color]
True
--
Chris Green <cmg@dok.org>
This is my signature. There are many like it but this one is mine.
Jay
Guest
 
Posts: n/a
#4: Jul 18 '05

re: detecting variable types


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" <peter@engcorp.com> wrote in message
news:Y-WdnWCkPIytTszcRVn-vQ@powergate.ca...[color=blue]
> Jay wrote:[color=green]
> > I'm sure this is a really dumb question, but how do you detect a[/color][/color]
variable[color=blue][color=green]
> > type in Python?
> >
> > For example, I want to know if the variable "a" is a list of strings or[/color][/color]
a[color=blue][color=green]
> > single string. How do I do this?[/color]
>
> 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[/color]


John Roth
Guest
 
Posts: n/a
#5: Jul 18 '05

re: detecting variable types



"Jay" <wjjeonk@hotmail.com> wrote in message
news:ciskpq$7f2$1@news-int.gatech.edu...[color=blue]
> 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?[/color]

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[color=blue]
>
>
> Jay.
>
>
> "Peter Hansen" <peter@engcorp.com> wrote in message
> news:Y-WdnWCkPIytTszcRVn-vQ@powergate.ca...[color=green]
>> Jay wrote:[color=darkred]
>> > I'm sure this is a really dumb question, but how do you detect a[/color][/color]
> variable[color=green][color=darkred]
>> > type in Python?
>> >
>> > For example, I want to know if the variable "a" is a list of strings or[/color][/color]
> a[color=green][color=darkred]
>> > single string. How do I do this?[/color]
>>
>> 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[/color]
>
>[/color]

djw
Guest
 
Posts: n/a
#6: Jul 18 '05

re: detecting variable types


Jay wrote:
[color=blue]
> 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" <peter@engcorp.com> wrote in message
> news:Y-WdnWCkPIytTszcRVn-vQ@powergate.ca...[color=green]
>> Jay wrote:[color=darkred]
>> > I'm sure this is a really dumb question, but how do you detect a[/color][/color]
> variable[color=green][color=darkred]
>> > type in Python?
>> >
>> > For example, I want to know if the variable "a" is a list of strings or[/color][/color]
> a[color=green][color=darkred]
>> > single string. How do I do this?[/color]
>>
>> 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[/color][/color]


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

Tuure Laurinolli
Guest
 
Posts: n/a
#7: Jul 18 '05

re: detecting variable types


Jay wrote:
[color=blue]
> 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?[/color]

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
Andrew Koenig
Guest
 
Posts: n/a
#8: Jul 18 '05

re: detecting variable types


"Jay" <wjjeonk@hotmail.com> wrote in message
news:ciskpq$7f2$1@news-int.gatech.edu...
[color=blue]
> 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?[/color]

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.


Grant Edwards
Guest
 
Posts: n/a
#9: Jul 18 '05

re: detecting variable types


On 2004-09-22, Andrew Koenig <ark@acm.org> wrote:[color=blue]
> "Jay" <wjjeonk@hotmail.com> wrote in message
> news:ciskpq$7f2$1@news-int.gatech.edu...
>[color=green]
>> 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?[/color]
>
> 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.[/color]

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??
Bengt Richter
Guest
 
Posts: n/a
#10: Jul 18 '05

re: detecting variable types


On Wed, 22 Sep 2004 15:45:55 -0400, "Jay" <wjjeonk@hotmail.com> wrote:
[color=blue]
>Thanks, Peter.[/color]
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.
[color=blue]
>
>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?
>[/color]
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,
[color=blue][color=green][color=darkred]
>>> class S(str): pass[/color][/color][/color]
...[color=blue][color=green][color=darkred]
>>> s=S('hello')
>>>
>>> s[/color][/color][/color]
'hello'[color=blue][color=green][color=darkred]
>>> type('hello')[/color][/color][/color]
<type 'str'>[color=blue][color=green][color=darkred]
>>> type(s)[/color][/color][/color]
<class '__main__.S'>[color=blue][color=green][color=darkred]
>>> isinstance('hello', str)[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> isinstance(s, str)[/color][/color][/color]
True

IOW,[color=blue][color=green][color=darkred]
>>> type('hello')==str[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> type(s)==str[/color][/color][/color]
False

Sometimes you do actually need that distinction though.
[color=blue]
>
>Jay.
>
>
>"Peter Hansen" <peter@engcorp.com> wrote in message
>news:Y-WdnWCkPIytTszcRVn-vQ@powergate.ca...[color=green]
>> Jay wrote:[color=darkred]
>> > I'm sure this is a really dumb question, but how do you detect a[/color][/color]
>variable[color=green][color=darkred]
>> > type in Python?
>> >
>> > For example, I want to know if the variable "a" is a list of strings or[/color][/color]
>a[color=green][color=darkred]
>> > single string. How do I do this?[/color]
>>
>> 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[/color]
>
>[/color]

Regards,
Bengt Richter
Byron
Guest
 
Posts: n/a
#11: Jul 18 '05

re: detecting variable types


Hi Jay,

You can detect the type of a variable by using the "types" module that
comes with Python. Here is how to use it:

[color=blue][color=green][color=darkred]
>>> import types
>>> name = "Steven"
>>> if type(name) is types.StringType:[/color][/color][/color]
print "Yes, name is a string."


Hope this helps,

Byron
---


Jay wrote:[color=blue]
> 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?
>
>[/color]
Scott David Daniels
Guest
 
Posts: n/a
#12: Jul 18 '05

re: detecting variable types


Byron wrote:[color=blue]
> Hi Jay,
>
> You can detect the type of a variable by using the "types" module that
> comes with Python. Here is how to use it:
>
>[color=green][color=darkred]
> >>> import types
> >>> name = "Steven"
> >>> if type(name) is types.StringType:[/color][/color]
> print "Yes, name is a string."
>
>
> Hope this helps,
>
> Byron
> ---
>
>
> Jay wrote:
>[color=green]
>> 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?
>>
>>[/color][/color]

If you must switch on the type, use something like:

...
if isinstance(vbl, (string, unicode)):
vbl = [vbl]
for element in vbl:
...

There are many more kinds of iterables than kinds of strings,
and you'll get fewer surprises with the above.

-Scott David Daniels
Scott.Daniels@Acm.Org
Alex Martelli
Guest
 
Posts: n/a
#13: Jul 18 '05

re: detecting variable types


Andrew Koenig <ark@acm.org> wrote:
...[color=blue]
> I don't see anything particularly wrong with detecting the type this way:
>
> if isinstance(params[key1], list):[/color]

Better than checking type(...), but then, a lesser evil need not be
'right'.
[color=blue]
> Of course that won't work for other kinds of sequences, but if that's what
> you want, then that's what you want.[/color]

Sometimes people want what's not best for them, and sometimes they
believe they want something, but really don't. If somebody asks me
about how best to shoot his own foot, I may perhaps suggest weapon types
and suitable ammunition, but often I'd add some note about the problems
that often result by shooting one's own foot, even with the most
suitable choice of ammunition and weapon. ((Come on -- if somebody
asked __in a C++ group__ how best to code the equivalent of...:

if(is_circle(x)) draw_circle(x);
else if(is_square(x)) draw_square(x);
...
would you just mention how dynamic_cast may be used for the purpose,
without one word as to how a polymorphic x.draw() is better...?-))

Nevertheless, singling out _strings_ from other kinds of sequences and
iterables IS unfortunately needed sometimes. That's why built-in type
basestring exists: for the sole reason of allowing a typetest of the
form "if isinstance(whatever, basestring):...". Once strings (byte and
unicode kinds) are out of the way, all other sequences and iterables can
often be treated uniformly and polymorphically with a for loop or
whatever. A frequent idiom:

if isinstance(whatever, basestring):
whatever = [whatever]
try: whatever = iter(whatever)
except TypeError: whatever = iter([whatever])
for item in whatever:
...

this deals sensibly with being passed any iterable (singling out strings
as scalars even though they're iterable) OR any non-iterable (to be
treated as the equivalent singleton-iterable with that one item only).


Alex
Alex Martelli
Guest
 
Posts: n/a
#14: Jul 18 '05

re: detecting variable types


Scott David Daniels <Scott.Daniels@Acm.Org> wrote:
...[color=blue]
> if isinstance(vbl, (string, unicode)):[/color]

Nope! Use isinstance(vbl, basestring) instead. basestring exists for
that one and only reason... besides str (not 'string') and unicode, any
user-coded type that _wants_ to be "treated as a string for all intents
and purposes" may choose to inherit from basestring instead of object
(it's supposed to get no effect whatsoever from that _except_ from code
that does this specific kind of typechecking specifically).


Alex
Egbert Bouwman
Guest
 
Posts: n/a
#15: Jul 18 '05

re: detecting variable types


On page 208 in the cookbook Alex seems to say that the Borg class
should be subclassed. I don't see the necessity.
Just take the original four-lined Borg(), give it a suitable name,
maybe add some arguments, add oher methods, and instantiate it.
Or am I missing something again ?
egbert
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================
Alex Martelli
Guest
 
Posts: n/a
#16: Jul 18 '05

re: detecting variable types


Egbert Bouwman <egbert.list@hccnet.nl> wrote:
[color=blue]
> On page 208 in the cookbook Alex seems to say that the Borg class
> should be subclassed. I don't see the necessity.
> Just take the original four-lined Borg(), give it a suitable name,
> maybe add some arguments, add oher methods, and instantiate it.
> Or am I missing something again ?[/color]

If you need two classes with "singleton-like behavior", implementing
them as subclasses of Borg is preferable to "copy and paste ``reuse''"
for much the same reason real reuse is always preferable to c&p reuse,
whenever feasible. Should there ever be any useful maintenance to Borg
(e.g., make it a newstyle class, optimize it in some other way,
whatever!), if you have reused it properly you're in clover; if you have
copied and pasted, you're not. As usual, there are _no_ compensating
advantages for "copy and paste ``so-called reuse''" whatsoever.

_Once, and ONLY once_. ANY TIME you have the same functionality coded
twice, in two places, in two copies, that should raise your hackles.
Train yourself to feel DISGUST at such cases, and remove them
immediately whenever you observe them, in favour of a "once and only
once" occurrence of the functionality, reused in two or more places in
the proper ways (function calls, class instantiation, inheritance,
whatever). You'll be MUCH happier as a result of such a stance,
throughout your programming career.


Alex
Closed Thread