473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to detect what type a variable is?

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
15 2004
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
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

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

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Loose | last post by:
Hello, in order to use my scripts developed at home directly after upload, I wish to define a variable $root at begin of each script in a way that it always - be it here at home or on any other...
6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
1
by: mbasil7 | last post by:
Hi at all. Is there a way to detect with javascript the scrolling bar with of the browser? My problem is that the the following script assing to the pos variable the browser window size but...
5
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
2
by: Christian Blackburn | last post by:
Hi Gang, Can someone show me how to detect the name of the variable received by a procedure? I'd really appreciate it. I would like the following code to detect the name of the strStupid...
3
by: umdsasha | last post by:
So, basically, I need to detect whether an alert window was thrown. I can't find where it's thrown from but I need to disable a button only if there were no alert windows thrown. Any ideas? ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.