How to determine the bool between the strings and ints?
Ricardo Aráoz
ricaraoz at gmail.com
Sat Sep 8 14:25:41 EDT 2007
Steven D'Aprano wrote:
...
..
.
> You know, one or two examples was probably plenty. The other six or seven
> didn't add anything to your post except length.
>
> Also, type testing by equality is generally not a good idea. For example:
>
> class HexInt(int):
> """Like built-in ints, but print in hex by default."""
> def __str__(self):
> return hex(self)
> __repr__ = __str__
>
> You should be able to use a HexInt anywhere you can use an int. But not
> if your code includes something like this:
>
> if type(value) == int:
> do_something()
> else:
> print "Not an int!"
>
> (What do you mean my HexInt is not an int? Of course it is.)
>
> Better is to use isinstance(value, int). Better still is to do duck-
> typing, and avoid type() and isinstance() as much as possible.
>>> type(a) == HexInt
True
That's what I wanted (though I don't know if that's what the OP wanted).
BTW, sorry for the wasted bandwidth, didn't realize you might have such
a small bandwidth that seven lines would be a hassle. We should also
tell the blokes of the 'music' thread to stop it, I can imagine how mad
that must get you.
More information about the Python-list
mailing list