Comparing variable types

K

Kill Bill

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?
 
K

KefX

type(i) == said:
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
 
A

Andrew Bennetts

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?

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

Emile van Sebille

Kill Bill said:
type(i) == "<type 'float'>"

This compares the current type of i to type("string"), which is <type
'str'>.

You can do:
False 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:

Emile van Sebille
(e-mail address removed)
 
K

Kill Bill

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

KefX

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

- Kef
 
K

KefX

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
 
A

Andrew Bennetts

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

David Eppstein

Andrew Bennetts said:
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

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

David Eppstein

"Kill Bill said:
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.
 
P

Peter Hansen

Emile said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top