Silly type() problem

Try this:

import types
type('asdf') is types.StringType
type('asdf') is str

I am not sure where 'str' comes from, but types.StringType is easy enough to
understand...

jw

···

-----Original Message-----
From: Wayne Koorts [mailto:wkoorts@mweb.co.za]
Sent: Monday, March 24, 2003 10:06 AM
To: wxPython
Subject: [wxPython-users] Silly type() problem

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

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwindows.org

thanks, i used
if isinstance(x, str):

instead.

Thanks,
Wayne

···

On Mon, 24 Mar 2003 13:36:39 -0600, Wyant, Jaime wrote:

Try this:

import types
type('asdf') is types.StringType
type('asdf') is str

I am not sure where 'str' comes from, but types.StringType is easy
enough to
understand...

jw

-----Original Message-----
From: Wayne Koorts [mailto:wkoorts@mweb.co.za]
Sent: Monday, March 24, 2003 10:06 AM
To: wxPython
Subject: [wxPython-users] Silly type() problem

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

-------------------------------------------------------------------
--
To unsubscribe, e-mail: wxPython-users-
unsubscribe@lists.wxwindows.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwindows.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-
unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-
help@lists.wxwindows.org

"Wyant, Jaime" <jwyant@sfbcic.com> writes:

Try this:

import types
type('asdf') is types.StringType
type('asdf') is str

I am not sure where 'str' comes from, but types.StringType is easy enough to
understand...

str is a built-in factory (type/function hybrid thingie):

    >>> str
    <type 'str'>
    >>> print str.__doc__
    str(object) -> string
    
    Return a nice string representation of the object.
    If the argument is a string, the return value is the same object.
    >>> type(str)
    <type 'type'>
    >>>

So str supports both type checking, as in your example of type('asdf')
is str, as well as being callable and returning a string value.

···

--
Patrick K. O'Brien
Orbtech http://www.orbtech.com/web/pobrien
-----------------------------------------------
"Your source for Python programming expertise."
-----------------------------------------------