Hi,
Why doesn't this script work the way it seems it should?
x = 'hello'
test = type(x)
print test
if type(x) is "<type 'str'>":
print "It's a string!"
else:
print "oops"
The weird thing is that when I print x it outputs <type 'str'>
Regards,
Wayne
wkoorts@mweb.co.za
Wayne Koorts wrote:
Hi,
Why doesn't this script work the way it seems it should?
Because type(x) returns an object of type type, not a string. When you
print it, you get a sting that describes the object. What you are trying
to do is analogous to:
if 5 = "5":
print "it's a five!"
Also, using x is y means you are checking to see if x and y both are
bound to the same object, NOT objects of the same value. For instance:
"this is a string" == "this is a string: with more stuff"[:16]
1
"this is a string" is "this is a string: with more stuff"[:16]
0
"is" might work in thios case, as Python mihg only keep one copy of the
StringType around, but I don't know if you can count on that.
What you want to do is:
x = 'hello'
test = type(x)
print test
if type(x) == type("s"):
print "It's a string!"
else:
print "oops"
or
import types
type(x) == types.StringType
-Chris
-- Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
Wayne Koorts <wkoorts@mweb.co.za> writes:
Hi,
Why doesn't this script work the way it seems it should?
x = 'hello'
test = type(x)
print test
if type(x) is "<type 'str'>":
print "It's a string!"
else:
print "oops"
The weird thing is that when I print x it outputs <type 'str'>
When you print Python, in effect, prints the result of str(test).
Printing always prints a string. But type(x) is not a string, it is a
type object that has a __str__ method that returns "<type 'str'>".
What you probably want is this (but it depends on your version of
Python and whether you care about unicode):
if isinstance(x, str):
···
--
Patrick K. O'Brien
Orbtech http://www.orbtech.com/web/pobrien
-----------------------------------------------
"Your source for Python programming expertise."
-----------------------------------------------
Chris Barker <Chris.Barker@noaa.gov> writes:
"is" might work in thios case, as Python mihg only keep one copy of the
StringType around, but I don't know if you can count on that.
You can, and should. Modules are singletons. It is considered
somewhat better form to use:
type(x) is types.whatever
What you want to do is:
> x = 'hello'
> test = type(x)
> print test
> if type(x) == type("s"):
> print "It's a string!"
> else:
> print "oops"
or
import types
type(x) == types.StringType
This got hashed around on the python.dev list several months ago and
the conclusion was that identity tests were to be preferred over
equality tests for things like type comparisons or the types module.
So while you will find plenty of examples that look like the one you
gave here, and while it does work, it is not considered the current
preferred method. (And don't ask me to remember all the gory details
of the debate.)
Note that there is also a StringTypes (with an "s"), which works for
unicode as well as regular strings. It's a tuple, so you have to use
the "in" operator:
>>> types.StringTypes
(<type 'str'>, <type 'unicode'>)
>>> type(x) in types.StringTypes
1
>>>
I think this got backported to older versions of Python, but I can't
remember which.
Too-many-ways-to-test-types-ly yours,
Pat
···
--
Patrick K. O'Brien
Orbtech http://www.orbtech.com/web/pobrien
-----------------------------------------------
"Your source for Python programming expertise."
-----------------------------------------------