Peer review: Python encyphering script

Carel Fellinger cfelling at iae.nl
Thu Feb 28 09:44:40 EST 2002


Nick Mathewson <QnickQm at alum.mit.edu> wrote:
> In article <3c7c1a91.150875561 at menuhin.netfront.net>, A. Jones wrote:
...
>>             if type(blurb[z]) == "<type 'list'>": #Stops nesting
>> strings

> #It's bad style to rely in the string value of a type.  Intstead, you

Not only is it bad style, it does do what he thinks it does:)

   >>> type(1)
   <type 'int'>
   >>> type(1) == type("<type 'int'>")
   0
   >>> type("<type 'int'>")
   <type 'str'>
   >>> type(1)
   <type 'int'>
   >>> type(1) == "<type 'int'>"
   0

but

    >>> str(type(1)) == "<type 'int'>"
    1

Type() returns a type object, the <type 'int'> thingy is merely a
string representtation of that object.  Ofcourse they are not equal.
To make this test work, one has to force the type object to return
its string representation, either with str(), repr() or backticks.

And one of the reasons it's bad style, is that in a next version of
Python the string representation might change. (not likely though)
-- 
groetjes, carel



More information about the Python-list mailing list