Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Comparing variable types

0 views
Skip to first unread message

Kill Bill

unread,
Oct 25, 2003, 10:36:03 PM10/25/03
to
type(i) == "<type 'float'>"
this always returns false. How come?
type(i)returns <type 'float'> if i is a float so why isn't == working?


KefX

unread,
Oct 25, 2003, 10:44:46 PM10/25/03
to
>type(i) == "<type 'float'>"
>this always returns false. How come?
>type(i)returns <type 'float'> if i is a float so why isn't == working?

Use isinstance(), like this:
isinstance(i, float)
This will return True if i is a float, False if not.

- Kef

Emile van Sebille

unread,
Oct 25, 2003, 10:48:44 PM10/25/03
to

"Kill Bill" <bi...@kill.com> wrote in message
news:bnfbuj$10f3di$1...@ID-198839.news.uni-berlin.de...
> type(i) == "<type 'float'>"

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


Kill Bill

unread,
Oct 25, 2003, 10:50:21 PM10/25/03
to
Can you tell me where you found that method in the docs? I'm having trouble
navigating them, the java docs are so much easier to look at.

"KefX" <keflim...@aol.comNOSPAM> wrote in message
news:20031025224446...@mb-m07.aol.com...

KefX

unread,
Oct 25, 2003, 10:56:44 PM10/25/03
to
>What you probably want is:
>
>>>> import types
>>>> type(i) == types.FloatType
>True
>>>>

Using isinstance() as I described earlier is probably better because I think it
works better with the idea of unifying types and classes.

- Kef

KefX

unread,
Oct 25, 2003, 10:58:44 PM10/25/03
to
>Can you tell me where you found that method in the docs?

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

Andrew Bennetts

unread,
Oct 25, 2003, 10:45:47 PM10/25/03
to pytho...@python.org

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.


Andrew Bennetts

unread,
Oct 25, 2003, 11:00:06 PM10/25/03
to pytho...@python.org
On Sat, Oct 25, 2003 at 10:50:21PM -0400, Kill Bill wrote:
> Can you tell me where you found that method in the docs? I'm having trouble
> navigating them, the java docs are so much easier to look at.

Library Reference, section 2.1: Built-in Functions
http://python.org/doc/current/lib/built-in-funcs.html

-Andrew.


Kill Bill

unread,
Oct 26, 2003, 12:44:20 AM10/26/03
to
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.

"Andrew Bennetts" <andrew-p...@puzzling.org> wrote in message
news:mailman.103.1067137...@python.org...

Andrew Bennetts

unread,
Oct 26, 2003, 1:29:38 AM10/26/03
to pytho...@python.org
On Sun, Oct 26, 2003 at 12:44:20AM -0400, Kill Bill wrote:
> 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.

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.


David Eppstein

unread,
Oct 26, 2003, 1:24:10 AM10/26/03
to
In article <mailman.102.1067136...@python.org>,
Andrew Bennetts <andrew-p...@puzzling.org> wrote:

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

David Eppstein

unread,
Oct 26, 2003, 1:25:11 AM10/26/03
to
In article <bnfjf4$10gat3$1...@ID-198839.news.uni-berlin.de>,
"Kill Bill" <bi...@kill.com> wrote:

> 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.

Peter Hansen

unread,
Oct 26, 2003, 8:32:11 AM10/26/03
to
Emile van Sebille wrote:
>
> "Kill Bill" <bi...@kill.com> wrote in message
> news:bnfbuj$10f3di$1...@ID-198839.news.uni-berlin.de...
> > type(i) == "<type 'float'>"
>
> This compares the current type of i to type("string"), which is <type
> 'str'>.

(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

John J. Lee

unread,
Oct 26, 2003, 8:54:23 AM10/26/03
to
"Kill Bill" <bi...@kill.com> writes:
> "KefX" <keflim...@aol.comNOSPAM> wrote in message
> news:20031025224446...@mb-m07.aol.com...
[...]
> > Use isinstance(), like this:
[...]

> Can you tell me where you found that method in the docs? I'm having trouble
[...]

Rule of thumb: If you can't find it, it's in section 2 of the library docs.


John

Cameron Laird

unread,
Oct 26, 2003, 9:55:03 AM10/26/03
to
In article <eppstein-D9C542...@news.service.uci.edu>,

David Eppstein <epps...@ics.uci.edu> wrote:
>In article <bnfjf4$10gat3$1...@ID-198839.news.uni-berlin.de>,
> "Kill Bill" <bi...@kill.com> wrote:
>
>> 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.
.
.
.
Also
dir(dict)
--

Cameron Laird <cla...@phaseit.net>
Business: http://www.Phaseit.net

0 new messages