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 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
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
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
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
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
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
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
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
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.
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
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
>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
>
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
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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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()...
|
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...
|
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?
...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |