How to detect what type a variable is?

L

Leandro Ardissone

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
 
G

Grant Edwards

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.
 
S

SeanDavis12

Leandro said:
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
 
L

Leandro Ardissone

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..
 
L

Leandro Ardissone

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.
 
R

Roberto Bonvallet

Leandro said:
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,
 
D

Daniel Klein

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
 
E

eduardo.padoan

Leandro said:
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.
 
?

=?iso-8859-1?q?Luis_M._Gonz=E1lez?=

Leandro said:
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
 
G

Grant Edwards

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.
 
T

Tim Chase

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
 
E

Eduardo \EdCrypt\ O. Padoan

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
 
D

Dustan

Eduardo said:
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.
 
C

Calvin Spealman

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top