By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

How to detect what type a variable is?

P: n/a
Hi,

I want to know what type is a variable.
For example, I get the contents of an xml but some content is a list or
a string, and I need to know what type it is.

Thanks

Nov 29 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
On 2006-11-29, Leandro Ardissone <la********@gmail.comwrote:
I want to know what type is a variable. For example, I get the
contents of an xml but some content is a list or a string, and
I need to know what type it is.
>>x = 'asdf'
type(x)
<type 'str'>
>>i = 0
type(i)
<type 'int'>
>>>
--
Grant Edwards grante Yow! I Know A Joke!!
at
visi.com
Nov 29 '06 #2

P: n/a
On 2006-11-29, Grant Edwards <gr****@visi.comwrote:
On 2006-11-29, Leandro Ardissone <la********@gmail.comwrote:
>I want to know what type is a variable. For example, I get the
contents of an xml but some content is a list or a string, and
I need to know what type it is.
>>>x = 'asdf'
type(x)
<type 'str'>
>>>i = 0
type(i)
<type 'int'>
That makes me wonder how he manages to store Python objects in
xml.

--
Neil Cerutti
Nov 29 '06 #3

P: n/a

Leandro Ardissone wrote:
Hi,

I want to know what type is a variable.
For example, I get the contents of an xml but some content is a list or
a string, and I need to know what type it is.
type(variable)

Sean

Nov 29 '06 #4

P: n/a
great, thanks

And how I can compare this "<type 'str'>" output ?
I want to decide what to do if the var is an string and what to do if
not..

Tried with:
if type(artistList) == "<type 'list'>":

and
if type(artistList) == "list":

but nothing..

On Nov 29, 12:41 pm, Grant Edwards <gra...@visi.comwrote:
On 2006-11-29, Leandro Ardissone <lardiss...@gmail.comwrote:
I want to know what type is a variable. For example, I get the
contents of an xml but some content is a list or a string, and
I need to know what type it is.
>x = 'asdf'
type(x)
<type 'str'>
>i = 0
type(i)
<type 'int'>--
Grant Edwards grante Yow! I Know A Joke!!
at
visi.com
Nov 29 '06 #5

P: n/a
Thanks,

I don't store Python objects in xml, but I get an object from a library
that parses xml and converts it to objects.
On Nov 29, 12:43 pm, Neil Cerutti <horp...@yahoo.comwrote:
On 2006-11-29, Grant Edwards <gra...@visi.comwrote:
On 2006-11-29, Leandro Ardissone <lardiss...@gmail.comwrote:
I want to know what type is a variable. For example, I get the
contents of an xml but some content is a list or a string, and
I need to know what type it is.
>>x = 'asdf'
type(x)
<type 'str'>
>>i = 0
type(i)
<type 'int'>That makes me wonder how he manages to store Python objects in
xml.

--
Neil Cerutti
Nov 29 '06 #6

P: n/a
Leandro Ardissone wrote:
And how I can compare this "<type 'str'>" output ?
I want to decide what to do if the var is an string and what to do if
not..

Tried with:
if type(artistList) == "<type 'list'>":

and
if type(artistList) == "list":

but nothing..
type() doesn't return a string, it returns a type object.
You should try this:

if isinstance(artistList, list):
...

Cheers,
--
Roberto Bonvallet
Nov 29 '06 #7

P: n/a
great,

that is just what I need!

Thank you all!

--
Leandro Ardissone

On Nov 29, 1:29 pm, Roberto Bonvallet <Roberto.Bonval...@cern.ch>
wrote:
Leandro Ardissone wrote:
And how I can compare this "<type 'str'>" output ?
I want to decide what to do if the var is an string and what to do if
not..
Tried with:
if type(artistList) == "<type 'list'>":
and
if type(artistList) == "list":
but nothing..type() doesn't return a string, it returns a type object.
You should try this:

if isinstance(artistList, list):
...

Cheers,
--
Roberto Bonvallet
Nov 29 '06 #8

P: n/a
On 29 Nov 2006 08:25:35 -0800, "Leandro Ardissone"
<la********@gmail.comwrote:
>great, thanks

And how I can compare this "<type 'str'>" output ?
I want to decide what to do if the var is an string and what to do if
not..

Tried with:
if type(artistList) == "<type 'list'>":

and
if type(artistList) == "list":

but nothing..
Try it this way...
>>artistList = []
isinstance(artistList, list)
True
>>if isinstance(artistList, list):
print "I'm a list."

I'm a list.
>>>

Daniel Klein
Nov 29 '06 #9

P: n/a

Leandro Ardissone wrote:
Hi,

I want to know what type is a variable.
For example, I get the contents of an xml but some content is a list or
a string, and I need to know what type it is.
You should try to treat it as a list, catch the exceptions raise when
it is a string (problably ValueError, TypeError ou Attribute error,
depends on what are you doing), and then treat it as a string. This is
the BAFP (better ask for forgiveness than permission) style, and is
more accepted in duck-typing languages like Python then LBYL (look
before you leep) style.

Nov 29 '06 #10

P: n/a

Leandro Ardissone wrote:
great, thanks

And how I can compare this "<type 'str'>" output ?
I want to decide what to do if the var is an string and what to do if
not..

Tried with:
if type(artistList) == "<type 'list'>":

and
if type(artistList) == "list":

but nothing..

You shouldn't enclose "list" inside quotes.
This is the correct way:

if type(artistList) == list:
do something...

or as someone suggested:

if isinstance(l, list):
do something...
hope this helps.
Luis

Nov 29 '06 #11

P: n/a
On 2006-11-29, ed************@gmail.com <ed************@gmail.comwrote:
>
Leandro Ardissone wrote:
>Hi,

I want to know what type is a variable.
For example, I get the contents of an xml but some content is a list or
a string, and I need to know what type it is.

You should try to treat it as a list, catch the exceptions raise when
it is a string (problably ValueError, TypeError ou Attribute error,
depends on what are you doing),
As you said, it depends on what he's doing. The problem is
that strings act a lot like lists, and if he's using the subset
of list characteristics that are implimented by strings, there
won't _be_ an exception -- just wrong results.

In the past, I've almost alwasy had to look before I leap when
dealing with both strings and lists.

--
Grant Edwards grante Yow! How's it going in
at those MODULAR LOVE UNITS??
visi.com
Nov 29 '06 #12

P: n/a
>I want to know what type is a variable.
>
You should try to treat it as a list, catch the exceptions
raise when it is a string (problably ValueError, TypeError ou
Attribute error, depends on what are you doing), and then
treat it as a string. This is the BAFP (better ask for
forgiveness than permission) style

One might prefer to check for string-ness, as strings can
duck-type somewhat like lists:

my_list = ['my', 'brain', 'hurts']
my_string = 'Are you the brain specialist?'

for test in [my_list, my_string]:
try:
for thing in test:
process_list_item(thing)
except Exception: #whatever flavor you want
process_string(thing) # not called because
#strings are iterable
This gives the potentially-surprising result of iterating over
my_string and calling process_list_item() with each character in
the string, rather than raising some exception and calling
process_string(). Python does the right thing, but it can be
confusing when the duck-typing syntax is identical for the two,
despite a desire to treat them differently...perhaps a tree-like
list of lists and strings such as HTML/XML structure.

-tkc

Nov 29 '06 #13

P: n/a
>
One might prefer to check for string-ness, as strings can
duck-type somewhat like lists:

my_list = ['my', 'brain', 'hurts']
my_string = 'Are you the brain specialist?'

for test in [my_list, my_string]:
try:
for thing in test:
process_list_item(thing)
except Exception: #whatever flavor you want
The exception should be the one that process_list_item raises when it
receives a string instead of a list. if you want to treat strings and
list in different ways, maybe it means that you are doing different
operations on then, like appendind things to the list or whatever. If
not, than you maybe want to test the types.
process_string(thing) # not called because
#strings are iterable
What if you invert your code?
for test in [my_string, my_list]:
try:
process_string_item(thing)
#suppose process_string_item does some string operation on a
list and gets this
# exception - because if not, I see no meanning in distinguishing then
except ValueError:
for thing in test:
process_list_item(thing)

But maybe you have a reason to do things to a string that could be
done to a list without raising an exception, but you dont want to do
this to *that* list. My sugestion was to think if there is another
way, and maybe you are right.
--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com
Nov 29 '06 #14

P: n/a

Eduardo "EdCrypt" O. Padoan wrote:

One might prefer to check for string-ness, as strings can
duck-type somewhat like lists:

my_list = ['my', 'brain', 'hurts']
my_string = 'Are you the brain specialist?'

for test in [my_list, my_string]:
try:
for thing in test:
process_list_item(thing)
except Exception: #whatever flavor you want

The exception should be the one that process_list_item raises when it
receives a string instead of a list. if you want to treat strings and
list in different ways, maybe it means that you are doing different
operations on then, like appendind things to the list or whatever. If
not, than you maybe want to test the types.
process_string(thing) # not called because
#strings are iterable

What if you invert your code?
for test in [my_string, my_list]:
try:
process_string_item(thing)
#suppose process_string_item does some string operation on a
list and gets this
# exception - because if not, I see no meanning in distinguishing then
In case you had trouble reading the comments because it was too wide:
"suppose process_string_item does some string operation on a list and
gets this exception - because if not, I see no meanning in
distinguishing then"

Has it occurred to you that perhaps the OP wants to do very different
operations depending on whether or not the item is of type str or list?
And since a list can in many cases act very similarly to a string, the
best way to distinguish would be a type check, especially since the OP
knows what tools are being used to receive the output.
except ValueError:
for thing in test:
process_list_item(thing)

But maybe you have a reason to do things to a string that could be
done to a list without raising an exception, but you dont want to do
this to *that* list. My sugestion was to think if there is another
way, and maybe you are right.
--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com
Nov 30 '06 #15

P: n/a
On 29 Nov 2006 07:36:26 -0800, Leandro Ardissone <la********@gmail.comwrote:
Hi,

I want to know what type is a variable.
For example, I get the contents of an xml but some content is a list or
a string, and I need to know what type it is.

Thanks

--
http://mail.python.org/mailman/listinfo/python-list
In nearly all cases where someone asks this question, the situation
turns out to not call for it at all. Usually you simply aren't
thinking in the language you are using properly. You should usually
know what you are dealing with. If you don't, then either the
different types of objects you might have should be interchangable or
you should create a situation where you dont have such an ambiguality.
Perhaps you could change the design such that you always have a list,
even if its just a list of a single string.

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/
Nov 30 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.