python2.2: type('name') -> <type 'str'> ??

Tim Peters tim.one at home.com
Wed Jul 25 00:14:06 EDT 2001


[Francisco]
>  I just compiled python2.2a and I'm getting this strange response to the
> 'type' command:
>
> Python 2.2a1 (#2, Jul 24 2001, 12:24:09)
> [GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
> Type "copyright", "credits" or "license" for more information.
> >>> type('')
> <type 'str'>
>
>  while I expected to get <type 'string'>.
>
>  Am I missing something or I just messed up with the compilation??

No, that's the way it works (today).  Under the type/class unification
scheme, type names also work as constructors.  Since str() was always the
way to build a string in Python, that's a natural name for its type.  This
name is also different:

>>> type(1L)  # used to be "long int"
<type 'long'>
>>>

Note this cute consequence:

>>> x = 1L
>>> type(x)(3)
3L
>>>

That is, type() in 2.2a1 truly returns a constructor, and type(1L)(y) is the
same as long(y).  Likewise for

>>> type("abc")(42)
'42'
>>> type("abc") is str
1
>>> type(1L) is long
1
>>>

I'm not sure the type names will stay this way, but determining that is part
of what an alpha release is for.  On a scale of 1 to 12, were you merely
surprised by the change or truly upset <wink>?





More information about the Python-list mailing list