Use isinstance(), like this:
isinstance(i, float)
This will return True if i is a float, False if not.
- Kef
This compares the current type of i to type("string"), which is <type
'str'>.
You can do:
>>> i = 1.3
>>> type(i) == "<type 'float'>"
False
>>> str(type(i)) == "<type 'float'>"
True
> this always returns false. How come?
> type(i)returns <type 'float'> if i is a float so why isn't == working?
>
What you probably want is:
>>> import types
>>> type(i) == types.FloatType
True
>>>
Emile van Sebille
em...@fenx.com
"KefX" <keflim...@aol.comNOSPAM> wrote in message
news:20031025224446...@mb-m07.aol.com...
Using isinstance() as I described earlier is probably better because I think it
works better with the idea of unifying types and classes.
- Kef
It's a builtin, meaning it's in module __builtin__. You'll find them in the
library reference near the top under "built-in functions" or some such.
- Kef
Because "<type 'float'>" is a string :)
You want
import types
type(i) == types.FloatType
or
type(i) == type(1.0)
or in 2.2 and later you can simply do
type(i) == float
-Andrew.
Library Reference, section 2.1: Built-in Functions
http://python.org/doc/current/lib/built-in-funcs.html
-Andrew.
"Andrew Bennetts" <andrew-p...@puzzling.org> wrote in message
news:mailman.103.1067137...@python.org...
Again, you can find it in the library reference:
http://python.org/doc/current/lib/typesmapping.html
Although it isn't clear until you've read that that dictionaries are a
"mapping type", if you look in the index you'll find that both "dictionary
object" and "dictionary type, operations on" point you to that section.
I think you probably want to familiarise yourself with all of section 2 of
the Library Reference.
-Andrew.
But it's almost always preferable to do
isinstance(i,float)
because that allows subclasses of float to be used. If you really have
to test whether i is an unsubclassed float,
type(i) is float
would be a better choice than
type(i)==float
as it more accurately expresses the intent that only the precise float
type will be allowed.
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
> Where can I find all the methods for Dictionaries? They list some here, but
> is that all of them?
> http://www.python.org/doc/2.3.2/tut/node7.html#SECTION007140000000000000000
> I think it is, but I don't like how its written. I want it to be written
> you know by method, constuctors, like in the Java API. Anyone know what I'm
> talking about? Its so quick to skim through the API to see which method you
> are looking for. Not the case here.
Try typing help(dict) to the Python interpreter.
(Minor correction) Actually it compares it to the actual string
containing the letters "<type 'float'>", which is of course going
to get one nowhere.
The above would have worked if the OP had used
repr(type(i)) == "<type 'float'>"
but that is the absolute worst way of doing this...
-Peter
Rule of thumb: If you can't find it, it's in section 2 of the library docs.
John
Cameron Laird <cla...@phaseit.net>
Business: http://www.Phaseit.net