typechecks: just say no! (was Re: Determining Types)
Alex Martelli
aleax at aleax.it
Tue Sep 4 10:24:23 EDT 2001
"Fredrik Lundh" <fredrik at pythonware.com> wrote in message
news:FC%k7.752$sn6.161080 at newsc.telia.net...
> Skip Montanaro wrote:
> > I suppose it might be the way you've written it, but I don't actually
call
> > an extra function to perform the comparison. I just code the test
inline:
> >
> > if type(var) is not types.StringType:
> > var = str(var)
>
> oh, that's lame ;-)
>
> str does the same thing, but in C. from the documentation:
...so an unconditional
var = str(var)
is vastly preferable. Agreed!
> str(object)
>
> Return a string containing a nicely printable representation
> of an object. For strings, this returns the string itself.
>
> same goes for int(), float(), long(), and tuple() (but not list())
And that, of course, is because list is the only mutable
type out of this bunch.
Alex
More information about the Python-list
mailing list